我怎样才能将片段转换为上下文?

时间:2015-10-25 18:52:32

标签: java android android-fragments

这是我的java代码。没有错误,但当我运行这个我发现错误。如何解决它? 首先我将片段转换为context。当我构建此代码时没有错误但是当我运行它时会出现一些错误。

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

这是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) 

2 个答案:

答案 0 :(得分:0)

我认为答案与Context无关。我认为问题在于您在夸大布局之前尝试添加按钮。如果你放行

addListenerOnButton();

在扩充布局后进入onCreateView,程序运行正常。

答案 1 :(得分:0)

button1 null ,在null对象中调用(Button) button1.findViewById(R.id.button1);会导致 NullpointerException

  

引起: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:
  在com.nextappsbd.himu.firstpage.onCreate(firstpage.java:26)

<强>更新

进行更改:

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