[英]Android - NullPointerException - Unable to Start Activity ComponentInfo
[英]Android: Unable to start activity: NullPointerException?
一旦上一个活动的游戏结束,我就试图使用intent
在我的应用程序中打开一个New Activity
,如下所示。 我收到NullPointerException
,我做错了什么?
打开新活动的代码:
//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();
}
}
新活动:
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
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>
mathsgameresults.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);
...
文本视图未初始化。 检查您的XML文件( memorygameresults.xml
)是否存在具有这些ID的memorygameresults.xml
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.