2017-10-27 9 views
-3

クイズアプリケーションの作業が終了したらアプリケーションが停止しました。私は、MySQLデータベースを使用してAndroidアプリケーションのクイズを行うことを学んでいますが、最後の質問がスコアページに移動するときに問題が発生します。クイズアプリケーションの作業が終了したらアプリケーションが停止しました

10-28 01:02:25.816 31874から31874/com.example.nuvo.myapplication E/AndroidRuntime:致命的な例外:メイン プロセス:com.example.nuvo.myapplication、PID:31874 ジャワ.lang.RuntimeException: java.lang.NullPointerExceptionが でandroid.app.ActivityThread.performLaunchActivity(ActivityThread:活性 ComponentInfo {com.example.nuvo.myapplication/com.example.nuvo.myapplication.Complete}を開始できません。 java:2429) at android.app.ActivityThread.handleLau android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1283)android.osで でandroid.app.ActivityThread.access $ 800(ActivityThread.java:166) でnchActivity(ActivityThread.java:2493) 。 Handler.dispatchMessage(Handler.java:102) とandroid.os.Looper.loop(Looper.java:136) とandroid.app.ActivityThread.main(ActivityThread.java:5584) (java.lang.reflect)です。 Method.invokeNative(ネイティブメソッド) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller。 (ZygoteInit.java:1268) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) at dalvik.system.NativeStart.main(ネイティブメソッド) 原因:java.lang.NullPointerException android.app.Activity.performCreateで でcom.example.nuvo.myapplication.Complete.onCreate(Complete.java:18) (Activity.java:5442) android.app.Instrumentation.callActivityOnCreate(計器で。 java:1094) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393) at android.app.ActivityThread $ H.handleMessageでandroid.app.ActivityThread.access $ 800から android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) (ActivityThread.java:166) (ActivityThread.java:1283 android.app.ActivityThread.main(ActivityThread.java:5584)でandroid.os.Handler.dispatchMessage android.os.Looper.loopで(Handler.java:102) (Looper.java:136) で) java.lang.reflect.Method.invokeNative(ネイティブメソッド) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1268) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) at dalvik.system.NativeStart.main (ネイティブメソッド)

Complate。

int score = getIntent().getExtras().getInt("score"); 

あなたの完全な活動を開け、あなたの意図は、持っていなかったようだ:javaの

import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 

public class Complete extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_complete); 
     int score = getIntent().getExtras().getInt("score"); 


     AlertDialog.Builder builder = new AlertDialog.Builder(Complete.this); 
     builder.setTitle("Quiz Complete"); 
     builder.setMessage("Your Score is: " +score+ " out of 10"); 
     builder.show(); 


     builder.setPositiveButton("Play Again", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       //do things 
       Intent intent = new Intent(Complete.this, QuizActivity.class); 
       startActivity(intent); 

      } 
     }); 

     builder.setNegativeButton("Done", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       //do things 
      } 
     }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 

    } 
    public void restartQuiz(View view) 
    { 
     Intent intent = new Intent(Complete.this, QuizActivity.class); 
     startActivity(intent); 
    } 
    public void mainMenu(View view) 
    { 
     Intent intent = new Intent(Complete.this, MainActivity.class); 
     startActivity(intent); 
    } 

} 

QuizActivity.java

import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 

public class Complete extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_complete); 
     int score = getIntent().getExtras().getInt("score"); 


     AlertDialog.Builder builder = new AlertDialog.Builder(Complete.this); 
     builder.setTitle("Quiz Complete"); 
     builder.setMessage("Your Score is: " +score+ " out of 10"); 
     builder.show(); 


     builder.setPositiveButton("Play Again", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       //do things 
       Intent intent = new Intent(Complete.this, QuizActivity.class); 
       startActivity(intent); 

      } 
     }); 

     builder.setNegativeButton("Done", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       //do things 
      } 
     }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 

    } 
    public void restartQuiz(View view) 
    { 
     Intent intent = new Intent(Complete.this, QuizActivity.class); 
     startActivity(intent); 
    } 
    public void mainMenu(View view) 
    { 
     Intent intent = new Intent(Complete.this, MainActivity.class); 
     startActivity(intent); 
    } 

} 

答えて

0

このラインはあなたのコードで問題があります整数の「score」となり、その結果nullが返され、NullPointerExceptionが発生しました。このような問題を回避するには、別のメソッドgetInt(String key、int defaultValue)s.aを使用することを検討してください。

int score = getIntent().getExtras().getInt("score", 0); 

この方法では、Intentが値なしで到着した場合でも例外は発生しませんが、デフォルトでは0に設定されます。

+0

私はそれを試しましたが、まだ同じエラーがあります。 @Serj Ardovic –

関連する問題