-4

I am trying to make a button that goes into another activity but every time I press it the app crashes Here is the code from my MainActivity.java file:

package com.example.noot_a_normal_pc.kmtomiles;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import static android.widget.Toast.makeText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button buttonBegin, buttonExit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonBegin = (Button) findViewById(R.id.begin);
        buttonBegin.setOnClickListener(this);
        buttonExit = (Button) findViewById(R.id.exit);
        buttonExit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.equals(buttonExit)) {
            Toast.makeText(getApplicationContext(), "Why would you want to exit such a great app?", Toast.LENGTH_LONG).show();
        }
        else if (v.equals(buttonBegin)) {
            Intent intent = new Intent (this, question.class);
            startActivity(intent);
        }
    }

}

Here is the code for both of my buttons from my XML file:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Begin"
    android:id="@+id/begin"
    android:layout_marginTop="45dp"
    android:layout_below="@+id/textView"
    android:layout_alignRight="@+id/textView"
    android:layout_alignEnd="@+id/textView"
    android:clickable="true"
    android:background="#ffffff"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Exit"
    android:id="@+id/exit"
    android:layout_marginTop="27dp"
    android:layout_below="@+id/begin"
    android:layout_alignLeft="@+id/begin"
    android:layout_alignStart="@+id/begin"
    android:clickable="true"
    android:background="#ffffff" />

Here is my stacktrace message:

7-2477/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.noot_a_normal_pc.kmtomiles, PID: 2477
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.noot_a_normal_pc.kmtomiles/com.example.noot_a_normal_pc.kmtomiles.question}; have you declared this activity in your AndroidManifest.xml?
 at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1794)
 at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
 at android.app.Activity.startActivityForResult(Activity.java:3917)
 at android.app.Activity.startActivityForResult(Activity.java:3877)
 at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
 at android.app.Activity.startActivity(Activity.java:4200)
 at android.app.Activity.startActivity(Activity.java:4168)
 at com.example.noot_a_normal_pc.kmtomiles.MainActivity.onClick(MainActivity.java:33)
 at android.view.View.performClick(View.java:5198)
 at android.view.View$PerformClick.run(View.java:21147)
 at android.os.Handler.handleCallback(Handler.java:739)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5417)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
elmigue017
  • 343
  • 2
  • 12
  • 2
    http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – Mike M. Apr 10 '17 at 00:58
  • *"the app crashes with no error message"* no, there is an error message. Check the stacktrace and post it here – codeMagic Apr 10 '17 at 01:03
  • 1
    there is a error message, also question.class, is that a actual class, why are not following convention should be upper case Question.class – Remario Apr 10 '17 at 01:14
  • Someone correct me if I am wrong, but I don't think you should be using `equals()` to compare a view, rather you should be using `==`. http://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java – sam_c Apr 10 '17 at 01:15
  • No you are wrong, views are objects, not primitive, so only equals can be used. – Remario Apr 10 '17 at 01:18
  • 1
    make sure the question class is an Activity (or service、receiver) and registered in your AndroidManifest. By the way, you'd better use v.getId() == R.id.begin ( or use switch case) instead of equals. – jiashie Apr 10 '17 at 01:28

2 Answers2

3

Scope of this keyword is rather specific, use getContext() or getBaseContext() instead.or MainActivity.this

You have not declared your activity in your manifest.That is why you are getting the error. For example <activity android:name="YourActivityName" />

Also the way you are comparing possibility of a view is not exactly right, instead use a switch statement with v.getId() method then each case statement with their id property. case R.id.view_name.

Remario
  • 3,813
  • 2
  • 18
  • 25
0

As a programmer, you need to read and understand the error log given (when app crashed).

By having logcat, at line

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.noot_a_normal_pc.kmtomiles/com.example.noot_a_normal_pc.kmtomiles.question}; have you declared this activity in your AndroidManifest.xml?

It is said that you have not declared your activity in manifest. The easiest way to create an activity without declare in manifest is by click

File > New > Activity > Empty Activity

Android Studio will create both Java and XML file (layout), and of course it will declare your activity in Manifests.XML too.

Perhaps my answer will help you in future :)

Zarul Izham
  • 569
  • 5
  • 17