空对象引用上的setText()

我在下面初始化的textView对象上使用setText时遇到错误,由于某些原因您仍然可以看到错误。

MainActivity.Java

public class MainActivity extends AppCompatActivity {
    TextView txtConfirmed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtConfirmed = findViewById(R.id.txtConfirmed);
        txtConfirmed.setText("testing");
    }
}


snippet of the xml code
<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/txtLastUpdate"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:orientation="horizontal">

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

                <TextView
                    android:id="@+id/txtConfirmed"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="12345"
                    android:textSize="35sp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/confirmed"
                    android:textSize="25sp" />
            </LinearLayout>
            <TextView
                android:id="@+id/txtDeltaConfirmed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:layout_marginBottom="50dp"
                android:text="[+0]"/>
        </LinearLayout>
    </androidx.cardview.widget.CardView>

这是错误:

2020-05-09 18:09:20.822 22311-22311 / com.example.networking E / AndroidRuntime:FATAL EXCEPTION:main     流程:com.example.networking,PID:22311     java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.networking / com.example.networking.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.TextView.setText(java。 lang.CharSequence)'引用为空对象         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3271)         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410)         在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)         在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)         在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2017)         在android.os.Handler.dispatchMessage(Handler.java:107)         在android.os.Looper.loop(Looper.java:214)         在android.app.ActivityThread.main(ActivityThread.java:7397)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:920)      原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'         在com.example.networking.MainActivity.onCreate(MainActivity.java:23)         在android.app.Activity.performCreate(Activity.java:7824)         在android.app.Activity.performCreate(Activity.java:7813)         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3246)         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410)         在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)         在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)         在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2017)         在android.os.Handler.dispatchMessage(Handler.java:107)         在android.os.Looper.loop(Looper.java:214)         在android.app.ActivityThread.main(ActivityThread.java:7397)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:920)

评论
挂机
挂机

//像这样声明:

TextView txtConfirmed= (TextView)findViewById(R.id.txtConfirmed);

//然后在您的OnCreate()方法中:

txtConfirmed.setText("testing");
点赞
评论