致命异常:运行android程序时

时间:2015-10-28 04:05:48

标签: android

我是Android的新手,我现在正在学习制作Android应用程序。从udemy课程学习教程。事情进展顺利,但由于某些原因,我的应用程序每次启动时都会崩溃。

这是我让应用程序崩溃的部分。当我评论此行时,应用程序将运行,当我删除应用程序时会崩溃。

代码位于页面Simple_question.java第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) {

            }
        });
    }
}

3 个答案:

答案 0 :(得分:1)

您已使用questions.xml作为Simple_question.java的布局文件,其中您没有标识为tvxxtvyy的TextView导致NPE。见行

setContentView(R.layout.questions);

因此要解决它,你必须用

替换它
setContentView(R.layout.FrontPAge);

答案 1 :(得分:1)

simple_question.length将返回整数值。试试这个

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

答案 2 :(得分:1)

由于尝试在TextView中设置int值,因此崩溃了。

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

正确的方法:

  

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

     

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

     

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

以上是三种可以在TextView中设置整数值的方法。

看起来您的字符串数组可能为空。

尝试在values文件夹中声明arrays.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>

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

       </string-array>

        </resources>

希望它会有所帮助。