java - Android:无法启动 Activity :NullPointerException?

标签 java android android-intent nullpointerexception

一旦上一个 Activity 的游戏完成,我尝试使用 Intent 在我的应用程序中打开一个新 Activity ,如下所示。我收到 NullPointerException,我做错了什么?

打开新 Activity 的代码:

//when no cards are left
                if(totalcards==0){

                    //get the avg med and att values
                    getAverageMeditationValue();
                    getAverageAttentionValue();

                    if(getAverageMeditationValue() && getAverageAttentionValue()){

                    //add session detail to the SQLite DB
                    writeToDatabase();
                    //intent to open activty showing results of session
                    Intent t = new Intent(MemoryGame.this, com.example.brianapp.MemoryGameResults.class);
                    t.putExtra("singleScore", single);
                    // Start intent
                    startActivity(t);
                    //stop recording EEG data
                    device.close();
                    }
                }

新 Activity :

 public class MemoryGameResults extends Activity {
    //declare vars
    TextView tv1;
    TextView tv2;
    TextView tv3;
    TextView tv4;
    TextView tv5;
    TextView tv6;
    Session singleScore;
    LoginDataBaseAdapter loginDataBaseAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.memorygameresults);

        initialiseVars();

        setUpAllTextViews();


    }

    public void initialiseVars() {

        // the single score from the the meditation class
        singleScore = (Session) getIntent().getSerializableExtra(
                "singleScore");

        // Declaring textViews
        tv1 = (TextView) findViewById(R.id.medresults1);
        tv2 = (TextView) findViewById(R.id.medresults2);
        tv3 = (TextView) findViewById(R.id.medresults3);
        tv4 = (TextView) findViewById(R.id.medresults4);
        tv5 = (TextView) findViewById(R.id.medresults5);
        tv6 = (TextView) findViewById(R.id.medresults6);

    }

    /**
     * Sets text for all textviews
     * in activity
     */
    public void setUpAllTextViews(){

        // setting text for each text view
        tv1.setText("Results of session:");
        tv2.setText("Average Meditation Value: " + singleScore.getMeditation());
        tv3.setText("Maximum Meditation Value: " + singleScore.getMax());
        tv4.setText("Average Attention Value: " + singleScore.getAvgAttention());
        tv5.setText("Maximum Attention Value: " + singleScore.getMaxAttention());
        tv6.setText("Correct Answers: " + singleScore.getScore());
    }

    }

/**
 * Method that ensures user is returned to main menu when they press back
 * button
 */
@Override
public void onBackPressed() {
    Intent t = new Intent(MemoryGameResults.this, com.example.brianapp.MainMenu.class);
    startActivity(t);
}

}

当我尝试这样做时,出现以下错误:

08-24 10:56:44.326: E/AndroidRuntime(17074): FATAL EXCEPTION: main
08-24 10:56:44.326: E/AndroidRuntime(17074): Process: com.example.brianapp, PID: 17074
08-24 10:56:44.326: E/AndroidRuntime(17074): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brianapp/com.example.brianapp.MemoryGameResults}: java.lang.NullPointerException
08-24 10:56:44.326: E/AndroidRuntime(17074):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
08-24 10:56:44.326: E/AndroidRuntime(17074):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
08-24 10:56:44.326: E/AndroidRuntime(17074):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
08-24 10:56:44.326: E/AndroidRuntime(17074):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
08-24 10:56:44.326: E/AndroidRuntime(17074):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-24 10:56:44.326: E/AndroidRuntime(17074):    at android.os.Looper.loop(Looper.java:157)
08-24 10:56:44.326: E/AndroidRuntime(17074):    at android.app.ActivityThread.main(ActivityThread.java:5356)
08-24 10:56:44.326: E/AndroidRuntime(17074):    at java.lang.reflect.Method.invokeNative(Native Method)
08-24 10:56:44.326: E/AndroidRuntime(17074):    at java.lang.reflect.Method.invoke(Method.java:515)
08-24 10:56:44.326: E/AndroidRuntime(17074):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
08-24 10:56:44.326: E/AndroidRuntime(17074):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
08-24 10:56:44.326: E/AndroidRuntime(17074):    at dalvik.system.NativeStart.main(Native Method)
08-24 10:56:44.326: E/AndroidRuntime(17074): Caused by: java.lang.NullPointerException
08-24 10:56:44.326: E/AndroidRuntime(17074):    at com.example.brianapp.MemoryGameResults.setUpAllTextViews(MemoryGameResults.java:323)
08-24 10:56:44.326: E/AndroidRuntime(17074):    at com.example.brianapp.MemoryGameResults.onCreate(MemoryGameResults.java:51)
08-24 10:56:44.326: E/AndroidRuntime(17074):    at android.app.Activity.performCreate(Activity.java:5426)
08-24 10:56:44.326: E/AndroidRuntime(17074):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
08-24 10:56:44.326: E/AndroidRuntime(17074):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
08-24 10:56:44.326: E/AndroidRuntime(17074):    ... 11 more

与两项 Activity 相关的 list 部分:

 <activity
            android:name="com.example.brianapp.MemoryGame"
            android:label="Memory Game" >
            <intent-filter>
                <action android:name="com.example.brianapp.MemoryGame" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.brianapp.MemoryGameResults"
            android:label="Memory Game Results" >
            <intent-filter>
                <action android:name="com.example.brianapp.MemoryGameResults" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

ma​​thsgameresults.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

     <TextView
        android:id="@+id/medresults1"
        android:layout_width="290dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:textSize="20sp" >
    </TextView>


    <TextView
        android:id="@+id/medresults2"
        android:layout_width="290dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:background="@drawable/buttonshape"
        android:layout_marginTop="10dp"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:textColor="#FFFFFF"
        android:textSize="20sp" >
    </TextView>

    <TextView
        android:id="@+id/medresults3"
        android:layout_width="290dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:background="@drawable/buttonshape3"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:text=" "
        android:textColor="#FFFFFF"
        android:textSize="20sp" >
    </TextView>

    <TextView
        android:id="@+id/medresults4"
        android:layout_width="290dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:background="@drawable/buttonshape2"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:text=" "
        android:textColor="#FFFFFF"
        android:textSize="20sp" >
    </TextView>

    <TextView
        android:id="@+id/medresults5"
        android:layout_width="290dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:background="@drawable/buttonshape"
        android:layout_marginTop="10dp"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:textColor="#FFFFFF"
        android:textSize="20sp">
    </TextView>

    <TextView
        android:id="@+id/medresults6"
        android:layout_width="290dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:background="@drawable/buttonshape3"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:text=" "
        android:textColor="#FFFFFF"
        android:textSize="20sp" >
    </TextView>

       <Button
            android:id="@+id/btnMathsResultsViewgraph"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="12dp"
            android:onClick="Graph8Handler"
            android:text="View Graph "
            android:textSize="20sp" />

</LinearLayout>

最佳答案

问题出在这些行:

    tv1 = (TextView) findViewById(R.id.medresults1);
    tv2 = (TextView) findViewById(R.id.medresults2);
    ...

TextView 尚未初始化。检查您的 XML 文件 (memorygameresults.xml) 是否存在具有这些 ID 的 TextView 。

关于java - Android:无法启动 Activity :NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25470536/

相关文章:

java - Gradle - spring-security-taglibs :4. 0.4.RELEASE 正在内部下载 spring-jdbc-4.2.5.RELEASE

android - 如何在 ImageView 中显示图像?

android - 需要 personFields 掩码。请指定一个或多个有效路径

android - BroadcastReceiver 太慢?

java - 如何使用 Java Spring 的泛型类型来实现存储库模式

java - 更改和改进了从 Web Servlet 到 Web 服务的数据传递

java - 如何从 Activity 中调用类(class)

java - 无法在 Activity 之间切换

java - 由 Alarm Manager 触发的 Pending Intent 似乎会立即触发

java - 如何在 Windows Server 2008 上禁用 Java 中的所有网络连接