无法将携带游戏分数的 Textview 从一个 activity(阶段)转移到下一个 activity(阶段)
Not able to transfer Textview carrying game score from one activity(stage) to the next activity(stage)
所以我是初学者,我正在尝试制作一款没有数据库和多项活动的问答游戏。我想在所有活动中将评分系统作为文本视图获取,它应该根据正确或错误的答案进行更改。一个多星期以来,我一直在努力解决这个问题,但我认为我只是浪费了那段时间。这是示例代码。
这是我的 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)
在您的 MainActivity
中设置了一个 String
值,而在 Page1
class 中您想要接收一个 Integer
。
所以
- 要么将
MainActivity
中的代码更改为 intent.putExtra("score", score);
- 或在您的
Page1
到 String score = mintent.getStringExtra("score");
.
此外,您似乎在 Page1
Activity 中设置了错误的布局文件。也许您的意思是 setContentView(R.layout.activity_page1);
,而不是 _page6
?
所以我是初学者,我正在尝试制作一款没有数据库和多项活动的问答游戏。我想在所有活动中将评分系统作为文本视图获取,它应该根据正确或错误的答案进行更改。一个多星期以来,我一直在努力解决这个问题,但我认为我只是浪费了那段时间。这是示例代码。
这是我的 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)
在您的 MainActivity
中设置了一个 String
值,而在 Page1
class 中您想要接收一个 Integer
。
所以
- 要么将
MainActivity
中的代码更改为intent.putExtra("score", score);
- 或在您的
Page1
到String score = mintent.getStringExtra("score");
.
此外,您似乎在 Page1
Activity 中设置了错误的布局文件。也许您的意思是 setContentView(R.layout.activity_page1);
,而不是 _page6
?