1

我在通过片段中的回调集修改 TextView 中的文本时遇到问题:

我的片段看起来像这样:

公共类 GetStartedFragment 扩展 Fragment 实现 OnClickListener {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    View view = inflater.inflate(R.layout.myFragment, container, false);

    Button b1, b2, b3;
    b1 = (Button) view.findViewById(R.id.btn_myBtn);
    b1.setOnClickListener(this);


    return view;
}

@Override
public void onClick(View v) {
    TextView textView = (TextView) v.findViewById(R.id.txt_myTxtBox);
    textView.setText("Hi!");
}

}

在片段 xml 文件中,我有以下防御:

<Button
        android:id="@+id/btn_myBtn"
        android:text="@string/myBtn" />

<TextView android:id="@+id/txt_myTxtBox" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_weight="1"
    android:gravity="center" android:text="Foo" />

当我单击按钮时,我得到一个空指针异常,这是我的 LogCat 日志:

08-12 12:32:56.963: E/AndroidRuntime(1089): FATAL EXCEPTION: main
08-12 12:32:56.963: E/AndroidRuntime(1089): java.lang.NullPointerException
08-12 12:32:56.963: E/AndroidRuntime(1089):     at com.foo.bar.fragments.more.GetStartedFragment.onClick(GetStartedFragment.java:61)
08-12 12:32:56.963: E/AndroidRuntime(1089):     at android.view.View.performClick(View.java:4240)
08-12 12:32:56.963: E/AndroidRuntime(1089):     at android.view.View$PerformClick.run(View.java:17721)
08-12 12:32:56.963: E/AndroidRuntime(1089):     at android.os.Handler.handleCallback(Handler.java:730)
08-12 12:32:56.963: E/AndroidRuntime(1089):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-12 12:32:56.963: E/AndroidRuntime(1089):     at android.os.Looper.loop(Looper.java:137)
08-12 12:32:56.963: E/AndroidRuntime(1089):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-12 12:32:56.963: E/AndroidRuntime(1089):     at java.lang.reflect.Method.invokeNative(Native Method)
08-12 12:32:56.963: E/AndroidRuntime(1089):     at java.lang.reflect.Method.invoke(Method.java:525)
08-12 12:32:56.963: E/AndroidRuntime(1089):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-12 12:32:56.963: E/AndroidRuntime(1089):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-12 12:32:56.963: E/AndroidRuntime(1089):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

这是错误的来源:v.findViewById(...) 无法工作,因为v 是您按下的按钮,并且它不包含 TextView。因此,在调用 textView.setText(...); 时会出现 NullPointer 异常;

   @Override
    public void onClick(View v) {
        TextView textView = (TextView) v.findViewById(R.id.txt_myTxtBox);
        textView.setText("Hi!");
    }

我建议将此作为解决方案: 只需将 TextView 设为成员变量并在 onCreateView() 方法中对其进行初始化。

private TextView textView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    View view = inflater.inflate(R.layout.myFragment, container, false);

    Button b1, b2, b3;
    b1 = (Button) view.findViewById(R.id.btn_myBtn);
    b1.setOnClickListener(this);
    textView = (TextView) view.findViewById(R.id.txt_myTxtBox);

    return view;
}

@Override
public void onClick(View pressedButton) {
    textView.setText("Hi!");
}
于 2013-08-12T16:38:44.013 回答