我正在研究具有多个域的测验应用程序。首先,每个域将有10个问题。我正在研究第一个域的逻辑,因此我将主要复制其他域的算法。我的问题是,在进行测验时会在屏幕上显示分数,但我无法将其值带给congrats类,因此当该活动启动应用程序时会崩溃。
这是我的代码:
package com.example.iulian.quickquiz;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class info extends AppCompatActivity {
Qinfo qinfo = new Qinfo();
int score = 0;
int number = 0;
TextView intrbinfo;
TextView Score;
ImageView image;
Button ch1;
Button ch2;
Button ch3;
Button ch4;
String answer = qinfo.getAnswer(0);
Intent intent;
congrats congrats = new congrats();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
intrbinfo = findViewById(R.id.intrbinfo);
Score = findViewById(R.id.score);
image = findViewById(R.id.image);
ch1 = findViewById(R.id.ch1);
ch2 = findViewById(R.id.ch2);
ch3 = findViewById(R.id.ch3);
ch4 = findViewById(R.id.ch4);
intent = new Intent(info.this, congrats.class);
intrbinfo.setText(qinfo.getQuestion(0));
ch1.setText(qinfo.getChoice1(0));
ch2.setText(qinfo.getChoice2(0));
ch3.setText(qinfo.getChoice3(0));
ch4.setText(qinfo.getChoice4(0));
ch1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ch1.getText() == answer){
number++;
score++;
updateScore(score);
updateQuestion(number);
}
else{
Toast.makeText(info.this, "Wrong answer!", Toast.LENGTH_SHORT).show();
number++;
}
}
});
ch2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ch2.getText() == answer){
number++;
score++;
updateScore(score);
updateQuestion(number);
}
else{
Toast.makeText(info.this, "Wrong answer!", Toast.LENGTH_SHORT).show();
number++;
updateQuestion(number);
}
}
});
ch3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ch3.getText() == answer){
number++;
score++;
updateScore(score);
updateQuestion(number);
}
else{
Toast.makeText(info.this, "Wrong answer!", Toast.LENGTH_SHORT).show();
number++;
updateQuestion(number);
}
}
});
ch4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ch4.getText() == answer){
number++;
score++;
updateScore(score);
updateQuestion(number);
}
else{
Toast.makeText(info.this, "Wrong answer!", Toast.LENGTH_SHORT).show();
number++;
updateQuestion(number);
}
}
});
}
private void updateQuestion(int x){
if(x<10){
intrbinfo.setText(qinfo.getQuestion(x));
ch1.setText(qinfo.getChoice1(x));
ch2.setText(qinfo.getChoice2(x));
ch3.setText(qinfo.getChoice3(x));
ch4.setText(qinfo.getChoice4(x));
answer = qinfo.getAnswer(x);}
if(x >= 10){
info.this.startActivity(intent);
}
}
private void updateScore(int point){
Score.setText(Integer.toString(point));
}
public String getScore(){
return Score.getText().toString();
}
}
这是祝贺班
package com.example.iulian.quickquiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class congrats extends AppCompatActivity {
TextView congrats;
TextView urscore;
TextView scoreView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_congrats);
info info = new info();
ist ist = new ist();
geo geo = new geo();
tele tele = new tele();
congrats = findViewById(R.id.congrats);
urscore = findViewById(R.id.urscore);
scoreView = findViewById(R.id.score);
scoreView.setText(info.getScore());
}
}
logcat在崩溃时说的是这句话:
2019-01-10 20:11:58.878 15597-15597/com.example.iulian.quickquiz E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.iulian.quickquiz, PID: 15597
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iulian.quickquiz/com.example.iulian.quickquiz.congrats}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2812)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6311)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at com.example.iulian.quickquiz.congrats.onCreate(congrats.java:25)
at android.app.Activity.performCreate(Activity.java:6757)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2704)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2812)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6311)
at java.lang.reflect.Method.invoke(Native Method)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:872) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 2019-01-10 20:11:58.926 686-748 /? E / ThermalEngine:========= YouTube模式:0 ============== 2019-01-10 20:12:00.311 10646-10646 /? E / WidgetViewCreator:setBgColorForSmartBulletin被调用 2019-01-10 20:12:00.313 10646-10646 /? E / ActivityThread:无法找到com.lge.launcher2.smartbulletin的提供者信息
答案 0 :(得分:1)
使用
ch1.getText().toString().equals(anwser)
使用这样的Intent将数据传递到另一个活动
Intent i = new Intent(this, congrats.class);
i.putExtra("score", score);
startActivity(i);
然后将其从您的新活动中提取出来,如下所示:
Intent intent = getIntent();
String score= intent.getExtras().getString("score");