致命的例外。应用程序崩溃开始

时间:2017-08-08 19:14:14

标签: java android android-activity

请我是android开发的新手。我正在为ditel的android 6开发程序员的标志测验应用程序。虽然我一丝不苟地遵循源代码,但我似乎无法成功运行应用程序,因为它在出现后几乎立即崩溃。这是我得到的例外。我将不胜感激任何可以解决这个问题的帮助。

     FATAL EXCEPTION: main
                                                                       Process: com.example.israel.flagquiz, PID: 4644
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.israel.flagquiz/com.example.israel.flagquiz.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.israel.flagquiz.MainActivityFragment.updateGuessRows(android.content.SharedPreferences)' on a null object reference
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601)
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:178)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                           at android.os.Looper.loop(Looper.java:194)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5637)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.israel.flagquiz.MainActivityFragment.updateGuessRows(android.content.SharedPreferences)' on a null object reference
                                                                           at com.example.israel.flagquiz.MainActivity.onStart(MainActivity.java:62)
                                                                           at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1244)
                                                                           at android.app.Activity.performStart(Activity.java:6140)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2478)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2601) 
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:178) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                           at android.os.Looper.loop(Looper.java:194) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5637) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

MainActivity中的相关代码

@Override
protected void onStart(){
    super.onStart();

        if (preferencesChanged){
                MainActivityFragment quizFragment = (MainActivityFragment)
                        getSupportFragmentManager().findFragmentById(R.id.quizFragment);
            quizFragment.updateGuessRows(
                    PreferenceManager.getDefaultSharedPreferences(this));
            quizFragment.updateRegions(
                    PreferenceManager.getDefaultSharedPreferences(this));
            quizFragment.resetQuiz();
            preferencesChanged = false;
        }

主要活动片段中的相关代码

              public void updateGuessRows(SharedPreferences sharedPreferences){
       String choices = new String();
     choices = sharedPreferences.getString(MainActivity.CHOICES, null);
    guessRows = Integer.parseInt(choices) / 2;

    for (LinearLayout layout: guessLinearLayouts)
        layout.setVisibility(View.GONE);

    for (int row = 0; row < guessRows; row++)
        guessLinearLayouts[row].setVisibility(View.VISIBLE);
}

2 个答案:

答案 0 :(得分:1)

  

此行的sharedPreferences为空

try
{
    string path = "//*@id='form_780_0']/div[2]/div/div[3]/div";
    if(driver.FindElement(By.XPath(path)).Displayed) 
    {
        Assert.Fail("Fails");
    }
}
catch (Exception)
{
    //There is nothing here
}

// Some code here and this is executed after evaluating if condition
// even if element is displayed
  

在开始

中初始化sharedPreferences

答案 1 :(得分:0)

添加以下行以初始化您的共享偏好

如果在活动中使用

sharedPreferences = getSharedPreferences("MainActivity", Context.MODE_PRIVATE);

如果在片段中使用

sharedPreferences = getActivity().getSharedPreferences("MainActivity", Context.MODE_PRIVATE);

初始化后,您可以从共享首选项文件中读取数据。