gpt4 book ai didi

java - 我在抽屉导航的 fragment 标记内放置了一个按钮。单击按钮时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 09:41:33 27 4
gpt4 key购买 nike

当我将 setText 添加到 onclickListener 中时,应用程序崩溃。

buttonlar = container.findViewById(R.id.buttonlar);
text = container.findViewById(R.id.text);

View view = inflater.inflate(R.layout.fragment_first, container, false);
buttonlar = view.findViewById(R.id.buttonlar);
buttonlar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
text.setText("test");


}
});
return view;



}

日志猫

2019-07-14 23:49:09.953 16842-16842/com.medicalsix.doctorsix E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.medicalsix.doctorsix, PID: 16842
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.medicalsix.doctorsix.First$1.onClick(First.java:50)
at android.view.View.performClick(View.java:7251)
at android.view.View.performClickInternal(View.java:7228)
at android.view.View.access$3500(View.java:802)
at android.view.View$PerformClick.run(View.java:27843)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7116)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)

而不是

text.setText("test");

如果我使用它,就会发生这种情况。

Log.i("Test", "Test.");

您可以通过 github 链接访问我的项目的详细信息。

https://github.com/prensmiskin/DoctorSix/blob/master/app/src/main/java/com/medicalsix/doctorsix/First.java

fragment.xml代码

如果你知道的话,可以帮忙吗?

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".First"
android:orientation="vertical"
android:id="@+id/bir">


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

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

<Button
android:id="@+id/buttonlar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dentofasiyal deformite"
android:textSize="20sp"
android:background="@color/colorPrimary"
android:textColor="@android:color/black"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ssss"
android:id="@+id/text"
android:textSize="20sp"/>
<Button
android:id="@+id/legcdurlyvideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dumbbell Fly "
android:textSize="20sp"
android:background="@color/colorPrimary"
android:textColor="@android:color/black"/>
<Button
android:id="@+id/legcurlydvideo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dumbbell Fly "
android:textSize="20sp"
android:background="@color/colorPrimary"
android:textColor="@android:color/black"/>
<Button
android:id="@+id/legcurlyvidedo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dumbbell Fly "
android:textSize="20sp"
android:background="@color/colorPrimary"
android:textColor="@android:color/black"/>
<Button
android:id="@+id/legcurlyvidseo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dumbbell Fly "
android:textSize="20sp"
android:background="@color/colorPrimary"
android:textColor="@android:color/black"/>
<Button
android:id="@+id/legcurlyvideso"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dumbbell Fly "
android:textSize="20sp"
android:background="@color/colorPrimary"
android:textColor="@android:color/black"/>
<Button
android:id="@+id/legcurlyvaideos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dumbbell Fly "
android:textSize="20sp"
android:background="@color/colorPrimary"
android:textColor="@android:color/black"/>
<Button
android:id="@+id/legcurlsyvideos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dumbbell Fly "
android:textSize="20sp"
android:background="@color/colorPrimary"
android:textColor="@android:color/black"/>

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/cene"
android:adjustViewBounds="true"/>




<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="WebViewLayout"
android:id="@+id/webview"/>


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cene"
android:textSize="20sp"/>


</LinearLayout>




</ScrollView>










</FrameLayout>

最佳答案

我相信您应该从膨胀 View 中获取 TextView 而不是 container (无论是什么)。尝试以下操作,

        //buttonlar = container.findViewById(R.id.buttonlar);
//text = container.findViewById(R.id.text);

View view = inflater.inflate(R.layout.fragment_first, container, false);
buttonlar = view.findViewById(R.id.buttonlar);
//Use the inflated view to get the text (just like you did for buttonlar)
text = view.findViewById(R.id.text);
buttonlar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
text.setText("test");
}
});
return view;
}

关于java - 我在抽屉导航的 fragment 标记内放置了一个按钮。单击按钮时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57031160/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com