通过ButtonClick设置TextView时出现NullPointerException

时间:2013-08-12 16:37:07

标签: android android-fragments

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

我的片段看起来像这样:

public class GetStartedFragment extends 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)

1 个答案:

答案 0 :(得分: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!");
}