-1

My program keeps crashing at the Intent, can't seem to figure out what's wrong. Can anyone help? I have tried removing "final" from the context declaration, is there something that I have to manually import?

public class MainActivity extends AppCompatActivity {

    EditText answer;
    ImageButton flashcard;
    Context context = this;

    public void onClick(View v) {
                int ans = Integer.parseInt(answer.getText().toString());
                if (ans == 5) {
                    AlertDialog.Builder alert = new AlertDialog.Builder(context);
                    alert.setTitle("Answer is correct");

                    alert.setMessage("See another?")
                         .setCancelable(false)
                         .setPositiveButton("Yes",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            Intent i = new Intent(MainActivity.this, Main2Activity.class);
                                            startActivity(i);
                                        }
                                    })
                            .setNegativeButton("No",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            MainActivity.this.finish();
                                        }
                                    });
                    AlertDialog alertDialog = alert.create();
                    alertDialog.show();
                }
                else
                    Toast.makeText(MainActivity.this, "Answer is Incorrect, try again.", Toast.LENGTH_SHORT).show();
            }  
        });
    }
}

The error log is below

4 11:30:05.048 9569-9569/com.example.exam3 E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: com.example.exam3, PID: 9569
                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.exam3/com.example.exam3.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                 at com.example.exam3.Main2Activity.onCreate(Main2Activity.java:28)
                                                                 at android.app.Activity.performCreate(Activity.java:6679)
                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                 at android.os.Looper.loop(Looper.java:154) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Phantômaxx
  • 37,352
  • 21
  • 80
  • 110
Ann Nguyen
  • 21
  • 1
  • 5

3 Answers3

1

Do it this way:

flashCard.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click
            int ans = Integer.parseInt(answer.getText().toString());
            if (ans == 5) {
                AlertDialog.Builder alert = new AlertDialog.Builder(context);
                alert.setTitle("Answer is correct")
                     .setMessage("See another?")
                     .setCancelable(false)
                     .setPositiveButton("Yes",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        Intent i = new Intent(MainActivity.this, Main2Activity.class);
                                        startActivity(i);
                                    }
                                })
                        .setNegativeButton("No",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        MainActivity.this.finish();
                                    }
                                });
                AlertDialog alertDialog = alert.create();
                alertDialog.show();
            }
            else
                Toast.makeText(MainActivity.this, "Answer is Incorrect, try again.", Toast.LENGTH_SHORT).show();
         }
     });
Sanjog Shrestha
  • 827
  • 9
  • 16
0

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object

tells you that your Main2Activity is calling onSetClickListener on a null object the rest tells you that it happened in your onCreate method. The most likely problem is that your layout.xml file either doesn't define the appropriate view, or has a different identifier. Hard to tell since it has nothing to do with the code you posted.

David Berry
  • 40,337
  • 12
  • 82
  • 94
0

You need to define what flashCard is by findViewById(R.id.flash_card). R.id.flash_card being whatever the id is for that element in your xml layout file. Make sure you call findViewById(R.id.flash_card) before you call flashCard.setOnClickListener(new View.OnClickListener() { ...});

mondakuwar
  • 114
  • 5