我在Fragment 中遇到 Textview的问题。在我的片段xml中,我有ImageButtons
和TextView
。我希望如果我点击按钮,Textview
进行更改。我像往常一样做所有事情,但它崩溃了。
这是我的代码:
public class Tatlilar extends Fragment implements View.OnClickListener{
public Tatlilar() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tatlilar, container, false);
ImageButton imageButtonKalsonuc = (ImageButton) view.findViewById(R.id.imageButtonCikolata);
final TextView textViewKalsonuc = (TextView) view.findViewById(R.id.textViewSonuc);
imageButtonKalsonuc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"tıklandı", Toast.LENGTH_LONG).show();
textViewKalsonuc.setText("hello");
}
});
return view;
}
@Override
public void onClick(View v) {
}
}
这是我的错误日志:
FATAL EXCEPTION: main
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.azelirbrevo.glisemikindeks.Tatlilar$1.onClick(Tatlilar.java:44)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19761)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_alignParentTop="false"
android:layout_alignParentStart="false"
android:layout_alignParentLeft="false"
android:gravity="left"
android:layout_marginTop="70dp"
android:id="@+id/tableLayout">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton android:id="@+id/imageButtonCikolata"
android:layout_column="3"
android:src="@drawable/chocolate"
android:background="@null"
android:layout_width="70dp"
android:layout_height="70dp"
android:gravity="left"
android:layout_marginLeft="15dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="420dp">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textViewKaloriSonuc"
android:textAlignment="center" />
答案 0 :(得分:0)
您向
Text View
提供了错误的ID。您的xml
文件包含您使用的不同ID。改变这一点。
TextView textViewKalsonuc = (TextView) view.findViewById(R.id.textViewSonuc);
对此。
TextView textViewKalsonuc = (TextView) view.findViewById(R.id.textViewKaloriSonuc);