My app crashes when I hit BACK button on my second activity. Basically when I hit BACK button when in second activity it says "App crashed unexpectedly" and I need to hit OK, but then it goes back to first activity anyway. I'd like it to go to first activity without error or just leave the app at all.
Here is my 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();
}
}
And here is my second 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();
}
}
@EDIT
Below crash report:
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();
}
try this way.because you are not passing activity name for intent to next activity.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.