退出子活动后返回主活动将取消激活我的两个按钮

时间:2014-08-18 15:50:14

标签: android

所以,我现在正在通过Android上的大书呆子牧场工作,我已经完成了大部分第一个应用程序,很有趣,无论如何,但我遇到的问题是在实施一个作弊按钮之后对于测验,当我从作弊屏幕返回时,我的真假按钮不再起作用。

肯定有点击注册,它只是停止做任何事情。

还有一个错误,如果我将其水平转动,整个应用程序崩溃,但我会发布这两个活动的代码,看看我们得到了什么。

    private void checkAnswer(boolean userPressedTrue){
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();

        int messageResId=0;

        if(mIsCheater){
            messageResId=R.string.judgement_toast;
        } else {

            if (userPressedTrue==answerIsTrue) {
            messageResId=R.string.correct_toast;
            } else {
            messageResId=R.string.incorrect_toast;
            }

        Toast.makeText(this, messageResId, Toast.LENGTH_SHORT)
            .show();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(data==null){
            return;
        }
        mIsCheater=data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN, false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if(savedInstanceState!=null){
            mCurrentIndex=savedInstanceState.getInt(KEY_INDEX, 0);
        }
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate(Bundle) called");
        setContentView(R.layout.activity_quiz);

        mCheatButton=(Button)findViewById(R.id.cheat_button);
        mCheatButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(QuizActivity.this, CheatActivity.class);
                boolean answerIsTrue=mQuestionBank[mCurrentIndex].isTrueQuestion();
                i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, answerIsTrue);
                startActivityForResult(i,0);

            }
        });



        mTrueButton = (Button)findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(true);
            }
        });
        mFalseButton = (Button)findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(false);
            }
        });


    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        Log.i(TAG, "onSaveInstanceState");
        savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
    }

    package com.example.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CheatActivity extends Activity {

    private boolean mAnswerIsTrue;


    public static final String EXTRA_ANSWER_IS_TRUE = "com.example.geoquiz.answer_is_true";
    public static final String EXTRA_ANSWER_SHOWN="com.example.geoquiz.answer_shown";

    private TextView mAnswerTextView;
    private Button mShowAnswer;

    private void setAnswerShownResult(boolean isAnswerShown){
        Intent data = new Intent();
        data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
        setResult(RESULT_OK, data);
    }



    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);

        mAnswerIsTrue=getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

        mAnswerTextView =(TextView)findViewById(R.id.answerTextView);
        setAnswerShownResult(false);
        mShowAnswer=(Button)findViewById(R.id.showAnswerButton);
        mShowAnswer.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mAnswerIsTrue) {
                    mAnswerTextView.setText(R.string.true_button);
                } else {
                    mAnswerTextView.setText(R.string.false_button);
                }
                setAnswerShownResult(true);
            }
        });
    }


}

在检查logcat时,我发现了一个关于水平方向问题的空指针异常。

08-18 18:39:30.398: E/AndroidRuntime(24316): FATAL EXCEPTION: main
08-18 18:39:30.398: E/AndroidRuntime(24316): Process: com.example.geoquiz, PID: 24316
08-18 18:39:30.398: E/AndroidRuntime(24316): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.geoquiz/com.example.geoquiz.QuizActivity}: java.lang.NullPointerException
08-18 18:39:30.398: E/AndroidRuntime(24316):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
08-18 18:39:30.398: E/AndroidRuntime(24316):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
08-18 18:39:30.398: E/AndroidRuntime(24316):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3805)
08-18 18:39:30.398: E/AndroidRuntime(24316):    at android.app.ActivityThread.access$900(ActivityThread.java:139)
08-18 18:39:30.398: E/AndroidRuntime(24316):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1216)
08-18 18:39:30.398: E/AndroidRuntime(24316):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-18 18:39:30.398: E/AndroidRuntime(24316):    at android.os.Looper.loop(Looper.java:136)
08-18 18:39:30.398: E/AndroidRuntime(24316):    at android.app.ActivityThread.main(ActivityThread.java:5086)
08-18 18:39:30.398: E/AndroidRuntime(24316):    at java.lang.reflect.Method.invokeNative(Native Method)
08-18 18:39:30.398: E/AndroidRuntime(24316):    at java.lang.reflect.Method.invoke(Method.java:515)
08-18 18:39:30.398: E/AndroidRuntime(24316):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-18 18:39:30.398: E/AndroidRuntime(24316):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-18 18:39:30.398: E/AndroidRuntime(24316):    at dalvik.system.NativeStart.main(Native Method)
08-18 18:39:30.398: E/AndroidRuntime(24316): Caused by: java.lang.NullPointerException
08-18 18:39:30.398: E/AndroidRuntime(24316):    at com.example.geoquiz.QuizActivity.onCreate(QuizActivity.java:116)
08-18 18:39:30.398: E/AndroidRuntime(24316):    at android.app.Activity.performCreate(Activity.java:5248)
08-18 18:39:30.398: E/AndroidRuntime(24316):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
08-18 18:39:30.398: E/AndroidRuntime(24316):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
08-18 18:39:30.398: E/AndroidRuntime(24316):    ... 12 more

1 个答案:

答案 0 :(得分:0)

使用logcat.v诊断缺少按钮功能,并在logcat中打印mIsCheater的布尔值。在确定布尔值打印之后,以及在回答检查点时,我重写了checkAnswer逻辑以检查实际值是什么 - 之前的问题是在此代码中它只是检查如果变量存在,似乎。

无论如何,固定代码是

private void checkAnswer(boolean userPressedTrue){
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();

    int messageResId=0;

    if(mIsCheater==true){
        messageResId=R.string.judgement_toast;  
    }

        else if (userPressedTrue==answerIsTrue) {
            messageResId=R.string.correct_toast;
    } else {
            messageResId=R.string.incorrect_toast;
    }

        Log.v(TAG, Boolean.toString(mIsCheater));

    Toast.makeText(this, messageResId, Toast.LENGTH_SHORT)
        .show();
    }

横向问题是由于我有横向布局而没有在xml中声明作弊按钮。