添加一些三个按钮的监听器后,app无法运行

时间:2014-12-23 14:54:52

标签: java android eclipse

我刚刚开始学习Androdid编程,并提出了一些问题。我试图用textview和三个按钮(true_button,false_button,next_question)实现一个应用程序。该项目在AVD上运行良好。后来我添加了一个prev_button及其监听器,用于链接到上一个问题。我还为textview question_text_view添加了一个监听器。 Eclipse Indigo报告没有错误,但是当在AVD上运行时,该程序在启动后立即停止。问题出在哪儿?或者,如何在使用这样的IDE时找到程序中的问题?谢谢。

以下是该项目的一些计划。 (MainActivity.java,activity_main.xml,TrueFalse.java,strings.xml)

MainActivity.java
package com.example1.geoquiz;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {

    private Button mTrueButton;
    private Button mFalseButton;
    private Button mNextButton;
    private TextView mQuestionTextView;
    private Button mNextButton2;
    private Button mPrevButton;

    private TrueFalse[] mQuestionBank = new TrueFalse[] {
            new TrueFalse(R.string.question2, true),
            new TrueFalse(R.string.question3, false),
            new TrueFalse(R.string.question4, false),
            new TrueFalse(R.string.question5, true),
            new TrueFalse(R.string.question6, true),
    };

    private int mCurrentIndex = 0;

    private void updateQuestion() {
        int question = mQuestionBank[mCurrentIndex].getQuestion();
        mQuestionTextView.setText(question);
    }
    private void checkAnswer(boolean userPressedTrue) {
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
        int messageResId = 0;
        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 onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mQuestionTextView = (TextView)findViewById(R.id.question_text_view);


        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);
            }
        });

        mNextButton = (Button)findViewById(R.id.next_button);
        mNextButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                updateQuestion();
            }
        });

        /*Click the textview to get to next question.*/
        mNextButton2 = (Button)findViewById(R.id.question_text_view);
        mNextButton2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
                updateQuestion();
            }
        });

        mPrevButton = (Button)findViewById(R.id.prev_button);
        mPrevButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
                updateQuestion();

            }
        });
        updateQuestion();
    }

}




TrueFalse.java
package com.example1.geoquiz;
public class TrueFalse {

    private int mQuestion;
    private boolean mTrueQuestion;
    public TrueFalse(int question, boolean trueQuestion) {
        mQuestion = question;
        mTrueQuestion = trueQuestion;
    }

    public int getQuestion() {
        return mQuestion;
    }
    public void setQuestion(int question) {
        mQuestion = question;
    }
    public boolean isTrueQuestion() {
        return mTrueQuestion;
    }
    public void setTrueQuestion(boolean trueQuestion) {
        mTrueQuestion = trueQuestion;
    }
}
MainActivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/question_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="180dp"
            android:text="@string/true_button" />

        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/true_button"
            android:layout_alignLeft="@+id/true_button"
            android:layout_marginLeft="130dp"
            android:text="@string/false_button" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <Button
            android:id="@+id/next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/prev_button"
            android:layout_alignBottom="@+id/prev_button"
            android:layout_alignParentRight="true"
            android:layout_marginRight="45dp"
            android:text="@string/next_button" />

        <Button
            android:id="@+id/prev_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="44dp"
            android:text="@string/prev_button" />

    </RelativeLayout>

</RelativeLayout>



strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">GeoQuiz</string>
    <string name="question1">Shenzhen is near Hong Kong.</string>
    <string name="question2">The Pacific Ocean is larger than the Atlantic Ocean.</string>
    <string name="question3">The Suez Canal connects the Red Sea and the Indian Ocean.</string>
    <string name="question4">The source of the Nile River is in Egypt.</string>
    <string name="question5">The Amazon River is the longest river in the Americas.</string>
    <string name="question6">Lake Bakal is the world\'s oldest and deepest freshwater lake.</string>

    <string name="true_button">true</string>
    <string name="false_button">false</string>
    <string name="next_button">Next</string>
    <string name="prev_button">Prev</string>

    <string name="correct_toast">correct</string>
    <string name="incorrect_toast">incorrect</string>
</resources>

1 个答案:

答案 0 :(得分:3)

/*Click the textview to get to next question.*/ mNextButton2 = (Button)findViewById(R.id.question_text_view); 我刚才看到了什么?

TextView不是按钮。如果您正在使用TextView,则应该在代码中转换为TextView,而不是Button;并在布局xml资源文件中设置android:clickable="true"