-1

The above code executed without any problem.But when i added "mQuestionTextView.setText(question);" the app crashes on startup.I guess,Syntactically the instruction is correct.Need help

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

    ...}

The activity_main.XML file :

<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 file:

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;
}
}
Ash Win
  • 29
  • 7
  • show the code where you are setting the value. – Rathan Kumar Mar 28 '15 at 12:52
  • have you initialized textivew, and what is that question variable type try to post the code – Rathan Kumar Mar 28 '15 at 12:56
  • 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) }; I have sent that R.string.question... as an integer so that should be saved as an integer right?or am i wrong? – Ash Win Mar 28 '15 at 12:56
  • I am new to android programming so pardon me if i am wrong – Ash Win Mar 28 '15 at 12:57
  • edit the question and repost in a proper way – Rathan Kumar Mar 28 '15 at 12:58
  • and post log cat also – Rathan Kumar Mar 28 '15 at 12:59
  • Share your code where u are calling QuestionTextView.setText(question) method and post ur log cat message...not in comment!!! – Chandrakanth Mar 28 '15 at 13:01
  • My Idea is this: I have created a class(TrueFalse.java) that will hold the string resource's integer equivalent id and a boolean variable to say whether the question is true or false.i have created an array of TrueFalse Objects(mQuestionBank).I am trying to set the value in the textview using the integer equivalent of the string resource in the R.java file..But its apparently not working as planned – Ash Win Mar 28 '15 at 13:03
  • Nope i tried e.printstacktrace(); But nothing gets printed – Ash Win Mar 28 '15 at 13:24

2 Answers2

0

From your can what I understand is question is the int value. But TextView.setText() will accept string values only. So you have to try like this

mQuestionTextView.setText(question+"");
Chandrakanth
  • 3,711
  • 2
  • 18
  • 31
0

while calling setText(..) method you have to pass String value only if you pass any integer to it will checks the value in R.java.

so while calling the setText convert that integer to the String then your problem will be solved.

like,

textview.setText(5+"");

write like this, initialize array inside 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)
    };
Rathan Kumar
  • 2,567
  • 2
  • 17
  • 24
  • I saved the value of that string resource in the R.java file in that question variable.I passed the integer hoping that it will check in the R.java file for the ID.But it is not doing so in this case. – Ash Win Mar 28 '15 at 12:53
  • where you are setting the text show the code. – Rathan Kumar Mar 28 '15 at 12:55