gpt4 book ai didi

java - 当我在一项 Activity 中点击“返回”按钮时,我的应用程序崩溃了

转载 作者:太空宇宙 更新时间:2023-11-04 11:35:42 27 4
gpt4 key购买 nike

当我在第二个 Activity 中点击“返回”按钮时,我的应用程序崩溃了。基本上,当我在第二个 Activity 中点击“返回”按钮时,它会显示“应用程序意外崩溃”,我需要点击“确定”,但无论如何它都会返回到第一个 Activity 。我希望它能够毫无错误地进入第一个 Activity ,或者根本离开应用程序。

这是我的 MainActivity.java:

public class MainActivity extends AppCompatActivity {

TextView TV_English,TV_Polish;
Locale mylocale;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TV_English=(TextView)findViewById(R.id.TVEnglish);
TV_Polish=(TextView)findViewById(R.id.TVPolish);

//Set English Language
TV_English.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"English Language",Toast.LENGTH_SHORT).show();
setLanguage("en");
}
});

//Set Polish Language
TV_Polish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Język polski",Toast.LENGTH_SHORT).show();
setLanguage("pl");
}
});
}

//Change language Method
protected void setLanguage(String language){
mylocale=new Locale(language);
Resources resources=getResources();
DisplayMetrics dm=resources.getDisplayMetrics();
Configuration conf= resources.getConfiguration();
conf.locale=mylocale;
resources.updateConfiguration(conf,dm);
Intent refreshIntent=new Intent(MainActivity.this,MainActivity.class);
finish();
startActivity(refreshIntent);
}

//This method opens QuizActivity
public void startQuiz(View view) {
Intent mainIntent = new Intent(this, pl.nataliana.knowyourwhale.QuizActivity.class);
startActivity(mainIntent);
EditText txtname = (EditText) findViewById(R.id.textName);
String name = txtname.getText().toString();
Intent i = new Intent(this, pl.nataliana.knowyourwhale.QuizActivity.class);
i.putExtra("name", name);
startActivity(i);
}

boolean doubleBackToExitPressedOnce = false;

@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, R.string.exit_press_back_twice_message, Toast.LENGTH_SHORT).show();
}
}

这是我的第二个 Activity (QuizActivity.java):

//This is an app for Whale Quiz - after answering all questions you will know how much do you know about whales.
public class QuizActivity extends AppCompatActivity {

// variables
private int score = 0;
public String introMessage;
public TextView intro;
public RadioButton trueQ1;
public RadioButton falseQ1;
public RadioButton trueQ2;
public RadioButton falseQ2;
public RadioButton poland;
public RadioButton island;
public CheckBox A3a;
public CheckBox A3b;
public CheckBox A3c;
public CheckBox A3d;
public EditText textAnswer;


//this method is called when Start Quiz button is clicked
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
Intent intent = getIntent();
String name = intent.getExtras().getString("name");
intro = (TextView) findViewById(R.id.intro);
trueQ1 = (RadioButton) findViewById(R.id.trueQ1);
falseQ1 = (RadioButton) findViewById(R.id.falseQ1);
trueQ2 = (RadioButton) findViewById(R.id.trueQ2);
falseQ2 = (RadioButton) findViewById(R.id.falseQ2);
poland = (RadioButton) findViewById(R.id.Poland);
island = (RadioButton) findViewById(R.id.Island);
A3a = (CheckBox) findViewById(R.id.A3a);
A3b = (CheckBox) findViewById(R.id.A3b);
A3c = (CheckBox) findViewById(R.id.A3c);
A3d = (CheckBox) findViewById(R.id.A3d);
introMessage = getString(R.string.Hello) + " " + name + getString(R.string.initial);
intro.setText(introMessage);
}

//this method is called when the user answers 1st question
public void clickQ1(View view) {
if (trueQ1.isChecked()) {
score++;
}
}

//this method is called when the user answers 2nd question
public void clickQ2(View view) {
if (trueQ2.isChecked()) {
score++;
}
}

//those methods are called when the user answers 3rd question
public void onCheckboxClickedA(View view) {
if (A3a.isChecked()) {
score++;
}
}

public void onCheckboxClickedB(View view) {
if (A3b.isChecked()) {
score++;
}
}

public void onCheckboxClickedC(View view) {
if (A3c.isChecked()) {
//wrong answer - do nothing
}
}

public void onCheckboxClickedD(View view) {
if (A3d.isChecked()) {
score++;
}
}

//this method is called to check 4th question
public void freeWilly() {
EditText textAnswer = (EditText) findViewById(R.id.textAnswer);
String willy = textAnswer.getText().toString();
Boolean booleanWilly = willy.equalsIgnoreCase(getString(R.string.Willy));
if (booleanWilly) {
score++;
}
}

//this method is called when the user answers 5th question
public void clickQ5(View view) {
if (island.isChecked()) {
score++;
}
}

//this method is called when the user clicks submit button
public void submit(View view) {
freeWilly();
Toast.makeText(this, getString(R.string.score) + " " + score + getString(R.string.for7), Toast.LENGTH_LONG).show();
score = 0;
}

//this method is called when the user clicks answers button
public void answers(View view) {
trueQ1 = (RadioButton) findViewById(R.id.trueQ1);
falseQ1 = (RadioButton) findViewById(R.id.falseQ1);
trueQ2 = (RadioButton) findViewById(R.id.trueQ2);
falseQ2 = (RadioButton) findViewById(R.id.falseQ2);
A3a = (CheckBox) findViewById(R.id.A3a);
A3b = (CheckBox) findViewById(R.id.A3b);
A3c = (CheckBox) findViewById(R.id.A3c);
A3d = (CheckBox) findViewById(R.id.A3d);
poland = (RadioButton) findViewById(R.id.Poland);
island = (RadioButton) findViewById(R.id.Island);
textAnswer = (EditText) findViewById(R.id.textAnswer);
trueQ1.setTextColor(GREEN);
falseQ1.setTextColor(Color.RED);
trueQ2.setTextColor(GREEN);
falseQ2.setTextColor(Color.RED);
A3a.setTextColor(GREEN);
A3b.setTextColor(GREEN);
A3c.setTextColor(Color.RED);
A3d.setTextColor(GREEN);
poland.setTextColor(Color.RED);
island.setTextColor(GREEN);
textAnswer.setText(R.string.Willy);
textAnswer.setTextColor(GREEN);
}

boolean doubleBackToExitPressedOnce = false;

@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, R.string.exit_press_back_twice_message, Toast.LENGTH_SHORT).show();
}
}

@编辑

以下崩溃报告:

04-10 14:58:35.398 6442-6442/pl.nataliana.knowyourwhale D/AndroidRuntime: Shutting down VM
04-10 14:58:35.398 6442-6442/pl.nataliana.knowyourwhale W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4162ee00)
04-10 14:58:35.418 6442-6442/pl.nataliana.knowyourwhale E/AndroidRuntime: FATAL EXCEPTION: main
Process: pl.nataliana.knowyourwhale, PID: 6442
java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.nataliana.knowyourwhale/pl.nataliana.knowyourwhale.QuizActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2264)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313)
at android.app.ActivityThread.access$1100(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at pl.nataliana.knowyourwhale.QuizActivity.onCreate(QuizActivity.java:42)
at android.app.Activity.performCreate(Activity.java:5340)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2228)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313) 
at android.app.ActivityThread.access$1100(ActivityThread.java:141) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5333) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711) 
at dalvik.system.NativeStart.main(Native Method) 

最佳答案

  @Override
public void onBackPressed() {
super.onBackPressed();
Intent intent=new Intent(QuizActivity.this,MainActivity .class);
startActivity(intent);
finish();
}

尝试这种方式。因为您没有将 Intent 的 Activity 名称传递给下一个 Activity 。

关于java - 当我在一项 Activity 中点击“返回”按钮时,我的应用程序崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43323636/

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