gpt4 book ai didi

java - Android 中的问答游戏无法获得分数

转载 作者:行者123 更新时间:2023-12-02 00:58:51 30 4
gpt4 key购买 nike

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

这是代码

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)

最佳答案

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

intent.putExtra("score", score);

在另一个 Activity 中只需调用

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

关于java - Android 中的问答游戏无法获得分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53579202/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com