当我点击我的一项活动的返回按钮时,我的应用程序崩溃

纳坦萨尔达

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

这是我的 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();
    }
}

这是我的第二个活动(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();
    }

试试这种方式。因为您没有将活动名称传递给下一个活动的意图。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当我选择单选按钮时,我的应用程序崩溃了

当我触摸任何按钮时,我的应用程序都崩溃了

当我在活动上放置按钮时,Android应用程序崩溃,空活动正常运行

当我尝试打开活动时,Android应用程序崩溃

每当我参加活动时,应用程序就会崩溃

当我按下开始按钮时,我的应用程序崩溃了

当我尝试向其中添加图像按钮时,我的应用程序崩溃了

每当我从一项活动转移到另一项活动时,我的应用程序就会崩溃

当我打开另一个活动时,应用程序崩溃

当我单击列表视图项时,应用程序崩溃,而不是打开另一个活动

当我移到某个片段时,应用程序崩溃

当我开始一个新的意图时,我的应用程序崩溃

当我点击自动链接网址时,应用程序崩溃

当我点击运行应用程序时,android 服务消失了,当应用程序崩溃时也是如此

当我点击注销时,我的应用程序崩溃

Android Studio:当我点击提交按钮时应用程序强制关闭

当我在 mp3 播放器中多次点击播放按钮时,我的应用程序崩溃了

为什么当我点击搜索按钮时应用程序崩溃?

当我返回上一页时,我的应用程序崩溃了

当我点击按钮时应用程序关闭

当我尝试转到其他活动时,我的应用程序崩溃

当我转到另一个活动时,我的应用程序不断崩溃

当我尝试使用图像按钮转到第二个活动时应用程序崩溃

当我尝试将数据从一个活动传递到另一个活动时,我的应用程序崩溃

当我尝试打开蓝牙时应用程序崩溃

当我连续点击开始和停止动作时,我的应用程序崩溃了。我正在研究语音识别

当我单击“再次播放”按钮时,我的整个应用程序崩溃

由于 java.lang.NullPointerException,当我尝试从一个活动移动到另一个活动时,我的应用程序崩溃

当我打开游戏活动时,我的应用程序不断崩溃。我试图创建一个从主要活动打开的游戏活动