【发布时间】:2016-09-03 07:03:05
【问题描述】:
片段中的文本视图有问题。在我的片段 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" />
【问题讨论】:
-
发布你得到的错误日志,你的问题不清楚你想问什么。
-
那是因为,你的 textView 为空
-
由于您没有发布错误日志或布局,因此您的 textview 得到了错误的 id 并且为 null 或者 imagebutton 是
-
如果您发布崩溃日志,我们可以提供帮助,
-
我现在添加了错误日志。