2017-04-24 92 views
-1

我的程序不斷崩潰在意圖,似乎無法弄清楚什麼是錯的。誰能幫忙?我試圖從上下文聲明中刪除「final」,是否有我必須手動導入的東西?意圖不斷崩潰我的應用程序

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(); 
      } 
     }); 
    } 
} 

錯誤日誌低於

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)  
+2

「不斷崩潰」並沒有告訴我們多少。發佈您的完整logcat –

+0

發佈logcat – Kaushal28

+0

發佈logcat,以便我們可以找出你的代碼有什麼問題,但我想你沒有將Main2Activity添加到清單文件:/ –

回答

1

這樣來做:

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(); 
     } 
    }); 
0

產生的原因:顯示java.lang.NullPointerException:試圖調用虛擬 法「無效android.widget.ImageButton.setOnClickListener(android.view.View $ OnClickListener)' on null object

告訴你,你是Main2Activity空對象,其餘告訴你,它發生在你的onCreate方法上調用onSetClickListener。最可能的問題是您的layout.xml文件未定義適當的視圖或具有不同的標識符。很難說,因爲它與您發佈的代碼無關。

0

您需要定義什麼flashCardfindViewById(R.id.flash_card)R.id.flash_card是你的xml佈局文件中該元素的id。請致電findViewById(R.id.flash_card),然後再致電flashCard.setOnClickListener(new View.OnClickListener() { ...});