【问题标题】:Android fragment textview.settext with button click带有按钮单击的 Android 片段 textview.settext
【发布时间】:2016-09-03 07:03:05
【问题描述】:

片段中的文本视图有问题。在我的片段 xml 中,我有 ImageButtonsTextView。我希望如果我点击按钮,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 是
  • 如果您发布崩溃日志,我们可以提供帮助,
  • 我现在添加了错误日志。

标签: android textview fragment


【解决方案1】:

您给Text View 提供了错误的ID。您的 xml 文件包含与您使用的不同的 id。

改变这个。

TextView textViewKalsonuc = (TextView) view.findViewById(R.id.textViewSonuc);

到这里。

TextView textViewKalsonuc = (TextView) view.findViewById(R.id.textViewKaloriSonuc);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 2018-02-18
    • 2014-07-28
    • 1970-01-01
    相关资源
    最近更新 更多