将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;
}
Logcat
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链接获取我的项目的详细信息。
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>
答案 0 :(得分:1)
我相信您应该从膨胀视图中获取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;
}