【问题标题】:textView setText() Null Pointer Exception [Single Activity Application]textView setText() 空指针异常【单活动应用】
【发布时间】:2018-01-19 00:46:12
【问题描述】:

只有一个活动的简单应用程序在查找布局的 TextView 并将其显示为 null 时产生了问题。请调试与单击提交按钮时应用程序崩溃的原因相同的原因。我在 Stack Overflow 中尝试了许多解决方案,许多解决方案说视图可能位于指定的活动之外,但这里只有一个活动。所以研究对我没有帮助,现在我只剩下可以预测我可能犯的错误的开发人员了。

MainActivity.java

package me.thirumurugan.quiz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button submit;
    TextView result;
    RadioButton answer_a;
    EditText answer_b;
    CheckBox answer_c1, answer_c2, answer_c3, answer_c4;
    EditText answer_d;
    int correct = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        result = (TextView) findViewById(R.id.result);
        answer_a = (RadioButton) findViewById(R.id.answer_a);
        answer_b = (EditText) findViewById(R.id.answer_b);
        answer_c1 = (CheckBox) findViewById(R.id.answer_c1);
        answer_c2 = (CheckBox) findViewById(R.id.answer_c2);
        answer_c3 = (CheckBox) findViewById(R.id.answer_c3);
        answer_c4 = (CheckBox) findViewById(R.id.answer_c4);
        answer_d = (EditText) findViewById(R.id.answer_d);
        submit = (Button) findViewById(R.id.submit);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                correct = 0;
                if (answer_a.isChecked()){
                    correct++;
                }
                if (answer_b.getText().toString().equals("2020")){
                    correct++;
                }
                if (answer_c1.isChecked()&&answer_c2.isChecked()&&answer_c3.isChecked()&&!answer_c4.isChecked()){
                    correct++;
                }
                if (answer_d.getText().toString().toLowerCase().equals("arun jaitley")){
                    correct++;
                }
                result.setText("The Score is " + correct + " on 4!");
            }
        });

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="me.thirumurugan.quiz.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/welcome_messege"
        android:textAlignment="center"
        android:id="@+id/result"
        android:padding="@dimen/padding"
        android:textStyle="bold"
        android:textSize="16sp"
        android:background="@color/colorAccent"
        android:textColor="#FFF"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/padding"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/padding"
                android:text="@string/question_1"
                android:textColor="@color/colorPrimary"
                android:textStyle="bold" />

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <RadioButton
                    android:id="@+id/answer_a"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/answer_1_a" />

                <RadioButton
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/answer_1_b" />

            </RadioGroup>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/padding"
                android:layout_marginTop="@dimen/padding"
                android:text="@string/question_2"
                android:textColor="@color/colorPrimary"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/answer_b"
                android:hint="@string/answer_hint"
                android:textColorHint="@color/colorAccent"
                android:layout_width="match_parent"
                android:textSize="14sp"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/colorPrimary"
                tools:targetApi="lollipop" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/padding"
                android:layout_marginTop="@dimen/padding"
                android:text="@string/question_3"
                android:textColor="@color/colorPrimary"
                android:textStyle="bold" />

            <CheckBox
                android:id="@+id/answer_c1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/answer_3_a" />

            <CheckBox
                android:id="@+id/answer_c2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/answer_3_b" />

            <CheckBox
                android:id="@+id/answer_c3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/answer_3_c" />

            <CheckBox
                android:id="@+id/answer_c4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/answer_3_d" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/padding"
                android:layout_marginTop="@dimen/padding"
                android:text="@string/question_4"
                android:textColor="@color/colorPrimary"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/answer_d"
                android:hint="@string/answer_hint"
                android:textSize="14sp"
                android:textColorHint="@color/colorAccent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/colorPrimary"
                tools:targetApi="lollipop" />

            <Button
                android:id="@+id/submit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/colorPrimary"
                android:text="@string/submit"
                android:textColor="#FFFFFF"
                android:textStyle="bold"
                tools:targetApi="lollipop" />

        </LinearLayout>

    </ScrollView>

</LinearLayout>

跟踪中的错误消息如下:

08-10 21:58:06.301 20947-20947/me.thirumurugan.quiz E/AndroidRuntime: 致命例外:主进程:me.thirumurugan.quiz,PID:20947
java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)' 为空 对象引用 me.thirumurugan.quiz.MainActivity$1.onClick(MainActivity.java:52) 在 android.view.View.performClick(View.java:5637) 在 android.view.View$PerformClick.run(View.java:22429) 在 android.os.Handler.handleCallback(Handler.java:751) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6119) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

【问题讨论】:

  • 我刚刚复制了你的代码并粘贴到我的android studio中,它工作得很好,没有错误
  • 尝试清理和构建您的应用,并使用断点调试您的应用
  • 我同意 Ali 的观点,这里看起来没有任何问题。您应该进行清理并构建。
  • 不,没用!

标签: java android android-layout android-studio


【解决方案1】:

您需要进行清理和重建。您的代码没有任何问题。多个用户复制了代码并在他们的应用程序中成功使用它,包括我自己:

文本丢失,因为我没有您的尺寸或字符串文件。

正如其他人所建议的那样,您没有范围问题。您创建的任何匿名内部类都应该可以访问您的成员变量。

您的共享代码中有其他缺失的行。在这种情况下,社区无法解决您的问题,因为没有人能够复制它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 2011-04-07
    • 1970-01-01
    • 2012-04-20
    相关资源
    最近更新 更多