Android 从以前的活动获取值时出错
我在获取以前活动的值时遇到问题。我正在创建一个测验应用程序,使用彩色按钮作为选择。基本上,你只需要选择指令上所说的按钮。代码如下:Android 从以前的活动获取值时出错,android,android-intent,Android,Android Intent,我在获取以前活动的值时遇到问题。我正在创建一个测验应用程序,使用彩色按钮作为选择。基本上,你只需要选择指令上所说的按钮。代码如下: public class EasyMode2Activity extends AppCompatActivity { Intent g = getIntent(); // get values from EasyMode1Activity.java public int score = g.getIntExtra("score", 5); p
public class EasyMode2Activity extends AppCompatActivity {
Intent g = getIntent(); // get values from EasyMode1Activity.java
public int score = g.getIntExtra("score", 5);
public final int questionTotal = 5;
public int question = g.getIntExtra("questions", 5);
public int questionsAnswered = g.getIntExtra("questionsAnswered", 5);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_easy_mode2);
Button btn4 = (Button) findViewById(R.id.button4);
Button btn5 = (Button) findViewById(R.id.button5);
Button btn6 = (Button) findViewById(R.id.button6);
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
question++;
TextView scoreText = (TextView) findViewById(R.id.txtScore2);
scoreText.setText(scoreText.getText() + "" +score);
toResults(view);
}
});
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
question++;
TextView scoreText = (TextView) findViewById(R.id.txtScore2);
scoreText.setText(scoreText.getText() + "" +score);
toResults(view);
}
});
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
question++;
score++;
TextView scoreText = (TextView) findViewById(R.id.txtScore2);
scoreText.setText(scoreText.getText() + "" +score);
questionsAnswered++;
toResults(view);
}
});
TextView questionTxt = (TextView) findViewById(R.id.txtquestion_number2);
questionTxt.setText(questionTxt.getText() + " " +question+ " of " +questionTotal);
final Chronometer timer = (Chronometer) findViewById(R.id.chronometer2);
timer.start();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void toResults (View view) {
Intent i = new Intent (this, ResultsActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); // store the score
b.putInt("questionsAnswered", questionsAnswered);
b.putInt("question", question);
i.putExtras(b); // put your score to your next intent
startActivity(i);
}
}
此外,我还尝试在手机上运行该应用程序。它坠毁了。上一个活动(EasyModeActivity1-它们有类似的代码,但有初始值除外)可以工作,但一旦我单击该活动中的任何按钮,它应该转到EasyModeActivity2,但它会崩溃。放置以下行:
Intent g = getIntent(); // get values from EasyMode1Activity.java
public int score = g.getIntExtra("score", 5);
public final int questionTotal = 5;
public int question = g.getIntExtra("questions", 5);
public int questionsAnswered = g.getIntExtra("questionsAnswered", 5);
内部onCreate如下所示:
Intent g = getIntent(); // get values from EasyMode1Activity.java
if (g != null) {
score = g.getIntExtra("score", 5);
questionTotal = 5;
question = g.getIntExtra("questions", 5);
questionsAnswered = g.getIntExtra("questionsAnswered", 5);
}
就在这一行之后:
setContentView(R.layout.activity_easy_mode2);
通过时:
Intent intent = new Intent(this, DestinationActivity.class)
intent.putExtra("myStringKey", "My String value");//pass string
intent.putExtra("myBooleanKey", false);//pass boolean
intent.putExtra("myIntKey", 50);//pass integer
intent.putExtra("score", 50);
intent.putExtra("unanswered", 100);
intent.putExtra("questionsAnswered", 15);
startActivity(intent);
你能从你的logcat中提供具体的错误日志吗?是的,请发布完整的logcat并突出显示错误发生的位置。我如何发布logcat?那么我是否也将这些放在后续活动中?我在btn实例下面添加了它,并保留了
Intent g=getIntent();//从EasyMode1Activity.java public int score=g.getIntExtra(“score”,5)获取值;公共最终int问题总数=5;公共int问题=g.getIntExtra(“问题”,5);public int questionsAnswered=g.getIntExtra(“questionsAnswered”,5)密码>他们在哪里。顺便说一句,我再次运行了应用程序。它仍然崩溃。是的,你应该在所有活动中都这么做。在Android Studio的屏幕底部,您将看到一些选项卡,如终端、Todo、Android监视器、版本控制,只需单击Android监视器选项卡,应用程序崩溃时您将看到错误日志。把它复制并粘贴到你的问题中