无法在Android问答游戏中获得分数

时间:2018-12-02 10:02:57

标签: java android android-studio crash

我正在开发问答游戏,并创建了一种算法来显示每个问题以及答案的变体。最后,您将被带到“祝贺”课程,您可以在其中看到分数。问题是我无法显示分数,因为如果我告诉计算机这样做会崩溃,那么我猜该值尚未保存。

这是代码

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;

public class info extends AppCompatActivity {

int score = 0;
int i = 0;
int z = 0;
TextView intrbinfo;
ImageView image;
Button ch1;
Button ch2;
Button ch3;
Button ch4;
Qinfo qinfo = new Qinfo();
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);
    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);

    Something3();

}


public void Something(int x){ // set the questions
    intrbinfo.setText(qinfo.getQuestion(x));
    image.setBackgroundResource(qinfo.picsID[x]);
    ch1.setText(qinfo.getChoice1(x));
    ch2.setText(qinfo.getChoice2(x));
    ch3.setText(qinfo.getChoice3(x));
    ch4.setText(qinfo.getChoice4(x));
}

public void Something2(Button button, int y, int score)  //verify if the button chosen has the right answer, it is all general case
{
    if(button.getText().toString().equals(qinfo.getAnswer(y))){
        score++;

    }
}

public void Something3(){  //add events to buttons after questions were set
    Something(i);

    ch1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Something2(ch1, z, score);
            i++;
            if(i<10){
                Something3();}
            else if(i >= 10) {
                info.this.startActivity(intent);
            }
        }
    });

    ch2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Something2(ch2, i, score);
            i++;
            if(i<10){
                Something3();}
            else if(i >= 10) {
                info.this.startActivity(intent);

            }
        }
    });

    ch3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Something2(ch3, i, score);
            i++;
            if(i<10){
                Something3();}
            else if(i >= 10) {
                info.this.startActivity(intent);

            }
        }
    });

    ch4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Something2(ch4, i, score);
            i++;
            if(i<10){
                Something3();}
            else if(i >=
                    10) {
                info.this.startActivity(intent);

            }
        }
    });
}

public int Right(int z)
{
    z++;
    z = congrats.scoore;
    return z;
}

public void displayScore(TextView textView){
    textView.setText(String.valueOf(score));
}

}

这是崩溃时的logcat(全为红色):

2018-12-02 13:15:34.824 9491-23272/? E/DropboxRealtime: null InputStream
java.io.IOException: null InputStream
    at aplb.a(:com.google.android.gms@14798017@14.7.98 (040306-222931072):19)
    at aplb.a(:com.google.android.gms@14798017@14.7.98 (040306-222931072):39)
    at apjv.a(:com.google.android.gms@14798017@14.7.98 (040306-222931072):154)
    at com.google.android.gms.stats.service.DropBoxEntryAddedChimeraService.onHandleIntent(:com.google.android.gms@14798017@14.7.98 (040306-222931072):80)
    at drs.handleMessage(:com.google.android.gms@14798017@14.7.98 (040306-222931072))
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.os.HandlerThread.run(HandlerThread.java:61)

1 个答案:

答案 0 :(得分:0)

您必须在调用startActivity之前添加此行

intent.putExtra("score", score);

在其他活动中,只需调用

 Intent mIntent = getIntent();
 int score= mIntent.getIntExtra("score", 0);