1

I'm a total new on android and I'm learning at this time to make Android applications. Learn the tutorial from udemy course. Things were well, but for some reasons my application began to crash every time I launch it.

Here is the part where I get crash the app. When I commented this line the app will run, when I remove the app will get crash.

The code is in the page Simple_question.java line number 41.

 tvtotallength_yy.setText(simple_question.length);

Logcat

FrontPAge.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/frontLayout">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop"
        android:src="@drawable/background" />


    <Button
        android:layout_width="220dp"
        android:layout_height="45dp"
        android:textSize="20sp"
        android:text="@string/sq"
        android:id="@+id/bsq"
        style="?android:attr/borderlessButtonStyle"
        android:layout_marginTop="80dp"
        android:layout_centerHorizontal="true"
        android:background="@drawable/button_state"
        android:layout_gravity="center" />

    <Button
        android:layout_width="220dp"
        android:layout_height="45dp"
        android:textSize="20sp"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/tq"
        android:id="@+id/btq"
        android:layout_below="@id/bsq"
        android:layout_alignLeft="@id/bsq"
        android:layout_alignStart="@id/bsq"
        android:layout_marginTop="48dp"
        android:background="@drawable/button_state"/>

    <Button
        android:layout_width="220dp"
        android:layout_height="45dp"
        android:textSize="20sp"
        android:text="@string/seeother"
        android:id="@+id/bseeotherapps"
        android:layout_below="@id/btq"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        style="?android:attr/borderlessButtonStyle"
        android:background="@drawable/button_state"/>

    <Button
        android:layout_width="220dp"
        android:layout_height="45dp"
        android:textSize="20sp"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/rateapp"
        android:id="@+id/brateapp"
        android:layout_below="@id/bseeotherapps"
        android:layout_alignLeft="@id/bseeotherapps"
        android:layout_alignStart="@id/bseeotherapps"
        android:layout_marginTop="46dp"
        android:background="@drawable/button_state" />

</RelativeLayout>


questions.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/questions_ll"
    android:weightSum="100">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:gravity="center"
        android:background="#C2EBFF"
        android:layout_gravity="center_horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:text="XX/"
            android:textSize="14sp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/tvxx" />

        <TextView
            android:layout_width="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_height="wrap_content"
            android:text="YY"     
            android:textSize="14sp"
            android:textColor="#000000"
            android:id="@+id/tvyy" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:background="#EBFFC2"
        android:layout_weight="80"
        android:layout_height="0dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Question"
            android:id="@+id/tvquestion"
            android:textColor="#000000"
            android:padding="15dp"/>

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/scrollView" >

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="15dp"
                    android:textColor="#000000"
                    android:text="Press A button for Answer"
                    android:id="@+id/tvanswer" />
            </LinearLayout>
        </ScrollView>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_weight="12"
        android:gravity="center|center_horizontal"
        android:background="#C2EBFF"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/bottomLayout"
        android:weightSum="1">


        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/left_but"
            android:layout_margin="8dp"
            android:id="@+id/bleft"
            />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/answer_state"
            android:layout_margin="3dp"
            android:id="@+id/bshowanswer"
            />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@drawable/but_right"
            android:id="@+id/bright"
            android:layout_weight="0.15" />

    </LinearLayout>


</LinearLayout>

vlaue/Simple_question.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="simple_ques">
        <item>What is your name?</item>
        <item>Where do you live?</item>

    </string-array>
</resources>

**values/simple_answers.xml**

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="simple_ans">
        <item>My name is Rabindra.</item>/
        <item>I live in Tandi.</item>/

    </string-array>
</resources>

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="np.com.rabindraacharya.androidinterview" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".FrontPage"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Simple_question.java

package np.com.rabindraacharya.androidinterview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

/**
 * Created by Rabindra on 10/27/2015.
 */
public class Simple_question extends AppCompatActivity {
     TextView tvquestion,tvanswer, tvtotallength_yy,tvpresentindex_xx;
     ImageButton bleft, bshow, bright;
    String[] simple_question, simple_answer;
    int index;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.questions);
        //setting value  to our variable and Textview

        simple_question = getResources().getStringArray(R.array.simple_ques);
        simple_answer = getResources().getStringArray(R.array.simple_ans);

        onClickListener();


    }
    public void onClickListener(){
        index = 0;
        tvquestion = (TextView)findViewById(R.id.tvquestion);
        tvanswer = (TextView)findViewById(R.id.tvanswer);
        tvpresentindex_xx = (TextView)findViewById(R.id.tvxx);
        tvtotallength_yy = (TextView)findViewById(R.id.tvyy);

        tvquestion.setText(simple_question[index]);
        tvanswer.setText("Press \"A\" Button for the Answer");

***//problem on this line***
when this line is comment then the app will run other wise the app will get crash
    tvtotallength_yy.setText(simple_question.length);

               //index+1 is in INteger,so to change to string
        tvpresentindex_xx.setText(String.valueOf(index+1));




        bleft = (ImageButton) findViewById(R.id.bleft);
        bshow = (ImageButton) findViewById(R.id.bshowanswer);
        bright = (ImageButton) findViewById(R.id.bright);

        bleft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        bshow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        bright.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }
}
1
  • man bellow two responses are acceptable you have to Cast array length int to String and setContentView(R.layout.FrontPAge); insted of setContentView(R.layout.questions) other wise you will get nullpinter Commented Oct 28, 2015 at 4:48

3 Answers 3

1

You are getting Crash because of trying to set int value in TextView.

`TextView.setText(int);` // wrong 

Correct way to do it:

1) TextView.setText(Interger.toString(int));

2) TextView.setText(String.valueOf(int));

3) TextView.setText(""+int);

Above are three ways where you can set integer value in TextView.

It seems that your string array might bu null.

Try to declare arrays.xml in values folder.

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

        <string-array name="simple_ans">
           <item>My name is Rabindra.</item>/
           <item>I live in Tandi.</item>/

        </string-array>

       <string-array name="simple_ques">
          <item>What is your name?</item>
          <item>Where do you live?</item>

       </string-array>

        </resources>

Hope it will help.

Sign up to request clarification or add additional context in comments.

14 Comments

I tried with this code tvtotallength_yy.setText(String.valueOf(simple_question.length)); Again same problem
Where ? Which type of exception you are getting?
When i comment that line it work perfectly, if i remove comment tag then the app will crash. Simple_question.java Line 41 tvtotallength_yy.setText(String.valueOf(simple_question.length));
Please post your logcat
Please scroll up, there u will see the Tag LogCat
|
1

You have used questions.xml as the layout file for Simple_question.java in which you dont have a TextView with id tvxx and tvyy which causes an NPE .See the line

setContentView(R.layout.questions);

Thus to solve it, you have to replace that with

setContentView(R.layout.FrontPAge);

6 Comments

Scroll Down the FrontPage there u will see questions.xml which has TextView having ID tvxx and tvyy
try to Toast the value of simple_question.length before setting it in the textview and check whether the Toast shows..
Toast toast = Toast.makeText(getApplicationContext(),simple_question.length,Toast.LENGTH_LONG); toast.show(); Is this correct to toast simple.question.lenghth. And where to write this code? I paste it in the button of questions.xml, while click that button the app get crash
no no..you have to put this code just above tvtotallength_yy.setText(simple_question.length);
I am completely new on android programming. Just 2 day to start to learn the android by watching the tutorial. The link below is the LogCat snap , i.sstatic.net/TJarQ.png
|
1

simple_question.length will return integer value . Try this

tvtotallength_yy.setText(String.valueOf(simple_question.length));

2 Comments

@NcitCosmos post your LogCat please.
try to hardcode your simple_question like this . remove String[] simple_question and add String[] simple_question = {"question a","question b"}; after setContentView

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.