为什么我的申请会停止?

时间:2012-07-30 20:40:53

标签: android

我不知道什么导致我的应用程序崩溃,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)

2 个答案:

答案 0 :(得分:1)

好Matthagan,LogCat有致命的例外。它是红色的。现在你要做的是首先在致命后至少打印前4行所以人们都知道你得到了什么。如果你想看到确切的地点和日食,请转到包含你的包裹名称的致命的第一行。点击此行,它将带您到达确切位置。

这是可能的。

1)quizButton =(Button)findViewById(R.id.launchQuiz);返回null,以便获得空指针异常。这可能发生因为launchQuiz没有正确定义你的布局。

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

答案 1 :(得分:1)

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

相关问题