所以我是初学者,我试图在没有数据库和多项活动的情况下制作测验游戏。我希望将评分系统作为所有活动中的文本视图,并且应该根据正确或错误的答案进行更改。我现在已经试着弄清楚了一个多星期了,我只觉得我浪费了那个时间。这是一个示例代码。
这是我的MainActivity.java
public class MainActivity extends AppCompatActivity {
int score = 0;
TextView scored;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scored = (TextView) findViewById(R.id.score);
scored.setText(String.valueOf(score));
}
public void rightanswer(View view){
score = score + 5;
scored.setText(String.valueOf(score));
Intent intent = new Intent(this, Page1.class);
intent.putExtra("score",String.valueOf(score));
startActivity(intent);
}
}
这是我的activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<LinearLayout
android:layout_width="50dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="#05083e"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:id="@+id/tvLabelScore"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#f6f6f6"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/score"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#e5e5e5"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="rightanswer"
/>
</RelativeLayout>
然后我的Page1.java
public class Page1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page6);
TextView textresult = (TextView) findViewById(R.id.textResult);
Intent mintent = getIntent();
int score = mintent.getIntExtra("score",0);
textresult.setText(String.valueOf(score)); // **I get null point exception error on this line.**
}
}
最后是我的activity_page1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<LinearLayout
android:layout_width="50dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="#05083e"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:id="@+id/tvLabelScore"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#f6f6f6"
android:layout_marginTop="5dp"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textResult"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#e5e5e5"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="rightanswer"
/>
</RelativeLayout>
你能告诉我我做错了什么吗?...我在互联网上输入了不同答案的所有代码......我认为这一切都错了......如果可能,请帮帮忙。
编辑:我得到的错误
FATAL EXCEPTION: main
Process: com.sample.testost, PID: 14544
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.testost/com.appsworthyourtime.testost.Page6}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.sample.testost.Page1.onCreate(Page1.java:20)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
在MainActivity
您设置String
值时,Page1
课程中您希望获得Integer
。
所以
MainActivity
中的代码更改为intent.putExtra("score", score);
Page1
至String score = mintent.getStringExtra("score");
。此外,您似乎在Page1
活动中设置了错误的布局文件。也许你的意思是setContentView(R.layout.activity_page1);
,而不是_page6
?