我正在开发一个问答游戏,并创建了一种算法来显示每个问题以及答案的变体。最后,您将被带到“祝贺” 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@[email protected] (040306-222931072):19)
at aplb.a(:com.google.android.gms@[email protected] (040306-222931072):39)
at apjv.a(:com.google.android.gms@[email protected] (040306-222931072):154)
at com.google.android.gms.stats.service.DropBoxEntryAddedChimeraService.onHandleIntent(:com.google.android.gms@[email protected] (040306-222931072):80)
at drs.handleMessage(:com.google.android.gms@[email protected] (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/