-1

I try to create a one onclicklistener for multiple button and a take the following error

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

My Main activitiy class is this:

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);
        }
    };

}

My two resource files is:

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" />

And the 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

I have search on google and here button nothing works so far! Maybe is something obvious that i didn't see! Thanks for your attention!

EDIT: This is the line which I get the error:

mButton0.setOnClickListener(onClickListener);
KostasC
  • 1,076
  • 6
  • 20
  • 40
  • 1
    Which line is MainActivity.java:426? If you have a good IDE just clicking on `at com.example.calculatorrr.MainActivity.onCreate(MainActivity.java:426)` should take you to it – Richard Tingle Jun 27 '14 at 17:21

2 Answers2

3

In onClicklistener your textview tx1 in null,because that textview txt1 is part of fragment not activity_main.

So either use tx1 in activity_main layout. or use in onCreateView method of fragment.

Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
2

You can not access you fragment Button inside your activity . So please use inside only Fragment java class. YOu can set your Button click Listener inside PlaceholderFragment Fragment class not inside activity

Ramkailash
  • 1,852
  • 1
  • 23
  • 19