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