引起:onClickListener上的java.lang.NullPointerException

时间:2014-06-27 17:18:41

标签: android onclicklistener

我尝试为多个按钮创建一个onclicklistener,并采取以下错误

06-27 20:12:27.468: D/AndroidRuntime(4810): Shutting down VM
06-27 20:12:27.468: W/dalvikvm(4810): threadid=1: thread exiting with uncaught exception (group=0x415caba8)
06-27 20:12:27.468: E/AndroidRuntime(4810): FATAL EXCEPTION: main
06-27 20:12:27.468: E/AndroidRuntime(4810): Process: com.example.calculatorrr, PID: 4810
06-27 20:12:27.468: E/AndroidRuntime(4810): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculatorrr/com.example.calculatorrr.MainActivity}: java.lang.NullPointerException
06-27 20:12:27.468: E/AndroidRuntime(4810):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
06-27 20:12:27.468: E/AndroidRuntime(4810):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
06-27 20:12:27.468: E/AndroidRuntime(4810):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-27 20:12:27.468: E/AndroidRuntime(4810):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-27 20:12:27.468: E/AndroidRuntime(4810):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-27 20:12:27.468: E/AndroidRuntime(4810):     at android.os.Looper.loop(Looper.java:136)
06-27 20:12:27.468: E/AndroidRuntime(4810):     at android.app.ActivityThread.main(ActivityThread.java:5001)
06-27 20:12:27.468: E/AndroidRuntime(4810):     at java.lang.reflect.Method.invokeNative(Native Method)
06-27 20:12:27.468: E/AndroidRuntime(4810):     at java.lang.reflect.Method.invoke(Method.java:515)
06-27 20:12:27.468: E/AndroidRuntime(4810):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-27 20:12:27.468: E/AndroidRuntime(4810):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-27 20:12:27.468: E/AndroidRuntime(4810):     at dalvik.system.NativeStart.main(Native Method)
06-27 20:12:27.468: E/AndroidRuntime(4810): Caused by: java.lang.NullPointerException
06-27 20:12:27.468: E/AndroidRuntime(4810):     at com.example.calculatorrr.MainActivity.onCreate(MainActivity.java:426)
06-27 20:12:27.468: E/AndroidRuntime(4810):     at android.app.Activity.performCreate(Activity.java:5231)
06-27 20:12:27.468: E/AndroidRuntime(4810):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-27 20:12:27.468: E/AndroidRuntime(4810):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
06-27 20:12:27.468: E/AndroidRuntime(4810):     ... 11 more

我的主要活动课程是:

public class MainActivity extends ActionBarActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
        FrameLayout mframelayout = (FrameLayout)findViewById(R.id.container);

        Button mButton0 = (Button) mframelayout.findViewById(R.id.bt0);
        Log.i("test1", mButton0.toString());
        Button mButton1 = (Button) mframelayout.findViewById(R.id.bt1);
        Button mButton2 = (Button) mframelayout.findViewById(R.id.bt2);
        Button mButton3 = (Button) mframelayout.findViewById(R.id.bt3);
        Button mButton4 = (Button) mframelayout.findViewById(R.id.bt4);
        Button mButton5 = (Button) mframelayout.findViewById(R.id.bt5);
        Button mButton6 = (Button) mframelayout.findViewById(R.id.bt6);
        Button mButton7 = (Button) mframelayout.findViewById(R.id.bt7);
        Button mButton8 = (Button) mframelayout.findViewById(R.id.bt8);
        Button mButton9 = (Button) mframelayout.findViewById(R.id.bt9);
        Button mButtonmul = (Button) mframelayout.findViewById(R.id.bt_multiplication);
        Button mButtonplu = (Button) mframelayout.findViewById(R.id.bt_plus);
        Button mButtonmin = (Button) mframelayout.findViewById(R.id.btminus);
        Button mButtondiv = (Button) mframelayout.findViewById(R.id.bt_divide);

        mButton0.setOnClickListener(onClickListener);
        mButton1.setOnClickListener(onClickListener);
        mButton2.setOnClickListener(onClickListener);
        mButton3.setOnClickListener(onClickListener);
        mButton4.setOnClickListener(onClickListener);
        mButton5.setOnClickListener(onClickListener);
        mButton6.setOnClickListener(onClickListener);
        mButton7.setOnClickListener(onClickListener);
        mButton8.setOnClickListener(onClickListener);
        mButton9.setOnClickListener(onClickListener);
        mButtonmul.setOnClickListener(onClickListener);
        mButtonplu.setOnClickListener(onClickListener);
        mButtonmin.setOnClickListener(onClickListener);
        mButtondiv.setOnClickListener(onClickListener);

    }

private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView txtview = (TextView) findViewById(R.id.txt1);
            String message = txtview.getText().toString();
            switch (v.getId()) {
            case R.id.bt0:
                message = message + "0";
                break;
            case R.id.bt1:
                message = message + "1";
                break;
            case R.id.bt2:
                message = message + "2";
                break;
            case R.id.bt3:
                message = message + "3";
                break;
            case R.id.bt4:
                message = message + "4";
                break;
            case R.id.bt5:
                message = message + "5";
                break;
            case R.id.bt6:
                message = message + "6";
                break;
            case R.id.bt7:
                message = message + "7";
                break;
            case R.id.bt8:
                message = message + "8";
                break;
            case R.id.bt9:
                message = message + "9";
                break;
            case R.id.bt_multiplication:
                message = message + "*";
                break;
            case R.id.bt_plus:
                message = message + "+";
                break;
            case R.id.btminus:
                message = message + "-";
                break;
            case R.id.bt_divide:
                message = message + "/";
                break;
            }
            txtview.setText(message);
        }
    };

}

我的两个资源文件是:

activity_main.xml中:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.calculatorrr.MainActivity"
    tools:ignore="MergeRootFrame" />

和fragment_main.xml:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <TextView
            android:id="@+id/txt1"
            android:layout_width="match_parent"
            android:layout_height="66dp"
            android:background="@android:color/darker_gray"
            android:hint="0"
            android:textSize="40dp" />
........ all the other butttons

我在谷歌搜索,这里按钮到目前为止没有任何作用!也许是显而易见的,我没有看到!谢谢你的关注!

编辑: 这是我得到错误的一行:

mButton0.setOnClickListener(onClickListener);

2 个答案:

答案 0 :(得分:3)

onClicklistener中,您的textview tx1为null,因为 textview txt1属于fragment而非activity_main。< / p>

所以要么在activity_main布局中使用tx1。或者使用onCreateView片段方法。

答案 1 :(得分:2)

您无法在活动中访问片段Button。所以请在里面只使用Fragment java类。你可以在PlaceholderFragment Fragment类中设置你的Button Click Listener而不是在activity