0

我不知道是什么导致我的应用程序崩溃,logcat 说它从 MAIN 获得了一个致命的异常,我真的不知道这意味着我对这些东西很陌生。发生的情况是第一个活动加载正常,但是一旦我按下按钮加载第二个活动就会停止。抱歉,如果它的问题太长或已经被问到,我已经尝试了很多以前问过的问题。

显现:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".QuizActivity"
        android:theme="@android:style/Theme.NoTitleBar"
        android:exported="false"
         >
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT">

            </category>                
        </intent-filter>

    </activity>

    <activity
        android:name=".MainMenu"
        android:theme="@android:style/Theme.NoTitleBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>    
    </activity>

</application>

第一项活动

public class MainMenu extends Activity {
Button quizButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_mainmenu);

    quizButton = (Button)findViewById(R.id.launchQuiz);

    quizButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (v.getId() == quizButton.getId()) {
                Intent launchQuizActivity = new Intent(MainMenu.this, QuizActivity.class);
                MainMenu.this.startActivity(launchQuizActivity);

            }
        }
    });
}
}

这是第二个:

public class QuizActivity extends Activity {

Intent returnHome = new Intent(QuizActivity.this, matt.apps.quiz.MainMenu.class);

String questions[] = {"What month is it?","What OS is this?" };
String lChoices[] = {"July","IOS"};
String cChoices[] = {"August","Android"};
String rChoices[] = {"October", "Windows?"};
int answers[] = {R.id.lButton,R.id.cButton };

boolean correct = false;
int numCorrect = 0;
int numWrong = 0;
int btnClicked;
int i = -1;
int t = 0;

Button lButton;
Button cButton;
Button rButton;
Button nextButton;
TextView questionTV;
TextView greetingTV;



public boolean getCorrect(int id) {
    if (id == answers[i]) {
        correct = true;
    } else {
    correct = false;
    }
    return correct;
}

OnClickListener clicker = new OnClickListener() {
    //TODO change to method to handle answers
                @Override
                public void onClick(View v) {

                    btnClicked = v.getId();

                    if (btnClicked == R.id.nextButton) { // next button

                        i += 1;

                        if (i <= questions.length) {
                        questionTV.setText(questions[i]);
                        lButton.setText(lChoices[i]);
                        cButton.setText(cChoices[i]);
                        rButton.setText(rChoices[i]);
                        nextButton.setText("Next question");
                        } else {
                            questionTV.setText("You got " + numCorrect + " right and " + numWrong + " wrong.");
                            lButton.setText("");
                            cButton.setText("");
                            rButton.setText("");
                        } // end of next button 

                    } else { // answer buttons
                    if (i > -1) { 
                        if (i <= questions.length) {
                    if (getCorrect(btnClicked) == true) {
                        numCorrect += 1;
                        Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();
                    } else {
                        numWrong += 1;
                        Toast.makeText(getApplicationContext(), "Wrong!", Toast.LENGTH_SHORT).show();
                    }
                }
            } 
        }// end of answer buttons
                }   
            };
@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    greetingTV = (TextView)findViewById(R.id.greetingTV);
    questionTV = (TextView)findViewById(R.id.questionTV);
    lButton = (Button)findViewById(R.id.lButton);
    cButton = (Button)findViewById(R.id.cButton);
    rButton = (Button)findViewById(R.id.rButton);
    nextButton = (Button)findViewById(R.id.nextButton);

    //set on click listeners for buttons      


        questionTV.setText("Please click the start button to begin.");
        nextButton.setText("Start");

}
}

错误日志:

E/AndroidRuntime(  620): FATAL EXCEPTION: main
E/AndroidRuntime(  620): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{matt.apps.quiz/matt.apps.quiz.QuizActivity}: java.lang.NullPointerException
E/AndroidRuntime(  620):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
E/AndroidRuntime(  620):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
E/AndroidRuntime(  620):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
E/AndroidRuntime(  620):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
4

2 回答 2

1

你有一个 NullPointer(它在你的 logcat 中的 FATAL EXCEPTION 之后的第一行末尾这样说)。只需跟踪堆栈跟踪提到您的包名称的位置(现在它只显示到 android.app.ActivityThread) - 就像读取 Java 堆栈跟踪一样。

于 2012-07-31T02:55:24.363 回答
1

那么 Matthagan,LogCat 有一个致命的例外。它是红色的。现在你要做的是首先总是在 fatal 之后打印至少前 4 行,这样人们就知道你得到了什么。如果您想查看确切的位置和您在 Eclipse 中的位置,请转到包含您的包裹名称的致命下方的第一行。单击此行,它将带您到确切的位置。

这就是它可能的样子。

1) quizButton = (Button)findViewById(R.id.launchQuiz); 返回 null 所以你得到一个空指针异常。这可能是因为您的布局未正确定义 launchQuiz。

2) 您可能没有为布局中的元素指定高度和宽度。

于 2012-07-30T20:51:33.847 回答