【问题标题】:Cannot get the value for score in quiz app无法在测验应用程序中获取分数值
【发布时间】:2026-01-22 18:25:01
【问题描述】:

我正在开发一个包含多个域的测验应用程序。起初每个域将有 10 个问题。我正在研究第一个域的逻辑,所以我将主要复制其他域的算法。我的问题是,在进行测验时会在屏幕上显示分数,但我无法将它的值带到恭喜类,所以当该活动应该启动时应用程序崩溃。

这是我的代码:

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 的提供者信息

【问题讨论】:

  • 我看不到你打电话给updateScore的任何地方我想这是更新分数的方法。
  • 我在尝试修复此问题时删除并复制了很多内容,但我可能错过了,我正在打开 Android Studio 并将添加对该方法的调用。\
  • 如果您在方法updateScore 中设置断点,当您按下答案时会触发它吗?也不要使用== 来比较字符串。
  • 你也可以发布崩溃。可能无关。
  • 从崩溃日志中,您似乎在空视图上调用 getText。确保您已初始化应用程序内的所有视图。

标签: java android variables textview


【解决方案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");

【讨论】:

  • 我试过了,没有任何区别,现在我唯一要解决的就是将 Score textview 的字符串值传递给 congrats 类中的 textview