无法在 TextView 上设置文本。给出 NullPointerException

unable to setText on TextView. Giving NullPointerException

我正在 textview 上设置文本,但它给了我 NullPointerException

这是我的 XML 文件。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activitys.ScoreActivity">

    <ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp">

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

            <!--Score Card-->
            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_margin="16dp"
                app:cardCornerRadius="15dp"
                app:cardElevation="6dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="-10dp"
                    android:text="Your Score"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/scoreQTv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="60dp"
                    android:layout_marginBottom="10dp"
                    android:text="100/200"/>


            </androidx.cardview.widget.CardView>
   
        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的 activity 我在 TextView 上设置文本的地方

public class ScoreActivity extends AppCompatActivity {

    private TextView scoreQTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);
        scoreQTv = findViewById(R.id.scoreQTv);
        //and finded all widgets using findviewByID ...

        scoreQTv.setText("My Score 18");

  }

我无法在我的 TextView 上设置文本。如何设置文本,为什么会出现此错误?

当我检查我的布局文件时,我得到了 2 个同名文件。我认为这就是发生这种情况的原因。我有 2 个这样的文件:

  1. activity_score.xml
  2. activity_score.xml v21 写在名字下。

这是我的 LogCat:

021-09-11 13:33:18.729 3897-3897/com.vishal.kaitka.phodo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.vishal.kaitka.phodo, PID: 3897
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vishal.kaitka.phodo/com.vishal.kaitka.phodo.Activitys.ScoreActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.vishal.kaitka.phodo.Activitys.ScoreActivity.onCreate(ScoreActivity.java:32)
        at android.app.Activity.performCreate(Activity.java:7994)
        at android.app.Activity.performCreate(Activity.java:7978)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

在您的问题中,您有 2 个同名文件

1.activity_score.xml

2.activity_score.xmlv21

因此您可以删除 xml 布局并创建新的 activity:

新建 Activity 命名为 Anything You Want

并将所有 xml 代码粘贴到新 Activity 的 xml 布局

所有 java 代码到新 Activity java 文件

我想也许对你有帮助!