-4

this is my java code. there is no error but when i run this i find error.How to solve it? firstly i convert fragment to context.when i build this code there is no error but when i run this there is some error.

    public class firstpage extends Fragment {
    Button button1,button2,button3,button4,button5,button6;
    public firstpage() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addListenerOnButton();

    }


    public void addListenerOnButton() {

        final Context context = getActivity().getApplicationContext();

        button1 = (Button) button1.findViewById(R.id.button1);

        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context, page.class);
                startActivity(intent);

            }

        });


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.first_page, container, false);


        // Inflate the layout for this fragment
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

This is logcat:

10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime: FATAL EXCEPTION: main
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime: Process: com.nextappsbd.himu, PID: 8280
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nextappsbd.himu/com.nextappsbd.himu.MainActivity}: java.lang.NullPointerException
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2440)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:151)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:110)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5344)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:  Caused by: java.lang.NullPointerException
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at com.nextappsbd.himu.firstpage.addListenerOnButton(firstpage.java:35)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at com.nextappsbd.himu.firstpage.onCreate(firstpage.java:26)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.support.v4.app.Fragment.performCreate(Fragment.java:1939)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:988)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:511)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1174)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.app.Activity.performStart(Activity.java:5371)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2340)
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2440) 
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:110) 
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:193) 
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5344) 
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676) 
10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
Rami
  • 7,879
  • 12
  • 36
  • 66
Amit Saha
  • 39
  • 9

2 Answers2

0

I believe that the answer is not related to Context. I believe that the issue is that you are attempting to add a Button before you have inflated your layout. If you put the line

addListenerOnButton();

into onCreateView after you inflate your layout, the program should run fine.

0

button1 is null and calling (Button) button1.findViewById(R.id.button1); in null object will cause the NullpointerException.

Caused by: java.lang.NullPointerException 10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime: at com.nextappsbd.himu.firstpage.addListenerOnButton(firstpage.java:35) 10-26 02:09:48.725 8280-8280/com.nextappsbd.himu E/AndroidRuntime:
at com.nextappsbd.himu.firstpage.onCreate(firstpage.java:26)

UPDATE:

Make those changes:

public class firstpage extends Fragment {
    Button button1,button2,button3,button4,button5,button6;
    View mRootView; // declare your rootview as global variable


    public firstpage() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //remove init buttons from here, because the view is not yet created
    }


    public void addListenerOnButton() {

        final Context context = getActivity().getApplicationContext();

        button1 = (Button) mRootView.findViewById(R.id.button1);// here i make the change

        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(context, page.class);
                startActivity(intent);

            }

        });


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.first_page, container, false);


        addListenerOnButton();// add your listeners here

        return rootView;
    }
Rami
  • 7,879
  • 12
  • 36
  • 66