上面的代码执行没有任何问题。但是当我添加“mQuestionTextView.setText(question);”应用程序在启动时崩溃。我猜,从语法上讲,指令是正确的。需要帮助
public class MainActivity extends ActionBarActivity {
private TextView mQuestionTextView;
private TrueFalse[] mQuestionBank = {
new TrueFalse(R.string.question_africa,false),
new TrueFalse(R.string.question_americas,true),
new TrueFalse(R.string.question_asia,true),
new TrueFalse(R.string.question_mideast,false),
new TrueFalse(R.string.question_oceans,true)
};
private int mCurrentIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mQuestionTextView = (TextView) findViewById(R.id.question_tv);
int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question);
...}
activity_main.XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.geopquiz1.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/question_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:onClick="true_onClick"
android:id = "@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button_text"/>
<Button
android:onClick="false_onClick"
android:id = "@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button_text"/>
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_btn_text"/>
</LinearLayout>
</LinearLayout>
TrueFalse.java文件:
package com.example.geopquiz1;
public class TrueFalse {
private int mQuestion;
private Boolean mTrueQuestion;
public TrueFalse(int question,Boolean trueQuestion){
mQuestion = question;
trueQuestion = mTrueQuestion;
}
public int getQuestion() {
return mQuestion;
}
public void setQuestion(int question) {
mQuestion = question;
}
public Boolean isTrueQuestion() {
return mTrueQuestion;
}
public void setTrueQuestion(Boolean trueQuestion) {
mTrueQuestion = trueQuestion;
}
}
答案 0 :(得分:0)
我能理解的是问题是int值。但TextView.setText()
只接受字符串值。所以你必须尝试这样
mQuestionTextView.setText(question+"");
答案 1 :(得分:0)
在调用setText(..)
方法时,只有在传递任何整数时才必须传递String值,它将检查R.java中的值。
所以在调用setText时将该整数转换为String,那么你的问题就会得到解决。
等
textview.setText(5+"");
这样写,在oncreate中初始化数组。
private TrueFalse[] mQuestionBank;
private int mCurrentIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mQuestionBank = {
new TrueFalse(R.string.question_africa,false),
new TrueFalse(R.string.question_americas,true),
new TrueFalse(R.string.question_asia,true),
new TrueFalse(R.string.question_mideast,false),
new TrueFalse(R.string.question_oceans,true)
};