在Android中获取片段中的上下文

时间:2014-01-08 08:26:56

标签: android android-fragments android-dialog

我正在尝试使用片段内的Activity的上下文。我收到此错误

01-08 13:51:14.662: E/AndroidRuntime(10334): FATAL EXCEPTION: main
01-08 13:51:14.662: E/AndroidRuntime(10334): java.lang.NullPointerException
01-08 13:51:14.662: E/AndroidRuntime(10334):    at android.app.Dialog.<init>(Dialog.java:154)
01-08 13:51:14.662: E/AndroidRuntime(10334):    at android.app.Dialog.<init>(Dialog.java:131)
01-08 13:51:14.662: E/AndroidRuntime(10334):    at com.iween.newinfiniteapp.PendingApprovals$2.onClick(PendingApprovals.java:238)
01-08 13:51:14.662: E/AndroidRuntime(10334):    at android.view.View.performClick(View.java:4212)
01-08 13:51:14.662: E/AndroidRuntime(10334):    at android.view.View$PerformClick.run(View.java:17476)
01-08 13:51:14.662: E/AndroidRuntime(10334):    at android.os.Handler.handleCallback(Handler.java:800)
01-08 13:51:14.662: E/AndroidRuntime(10334):    at android.os.Handler.dispatchMessage(Handler.java:100)
01-08 13:51:14.662: E/AndroidRuntime(10334):    at android.os.Looper.loop(Looper.java:194)
01-08 13:51:14.662: E/AndroidRuntime(10334):    at android.app.ActivityThread.main(ActivityThread.java:5371)
01-08 13:51:14.662: E/AndroidRuntime(10334):    at java.lang.reflect.Method.invokeNative(Native Method)
01-08 13:51:14.662: E/AndroidRuntime(10334):    at java.lang.reflect.Method.invoke(Method.java:525)
01-08 13:51:14.662: E/AndroidRuntime(10334):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-08 13:51:14.662: E/AndroidRuntime(10334):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-08 13:51:14.662: E/AndroidRuntime(10334):    at dalvik.system.NativeStart.main(Native Method)

代码我在哪里收到错误

approval_button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    final Dialog dialog = new Dialog(context);//this is line no 238
                    dialog.requestWindowFeature(dialog.getWindow().FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.approve_box);
                    dialog.setCancelable(false);
                    Button approve = (Button) dialog.findViewById(R.id.approve);
                    Button cancel = (Button) dialog.findViewById(R.id.cancel);
                    cancel.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            dialog.dismiss();
                        }
                    });

                    dialog.show();

                }
            });

我在这里设置上下文

   public class PendingApprovals extends Fragment {

    final Context context = getActivity();


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

2 个答案:

答案 0 :(得分:1)

执行时,您的片段可能尚未附加到您的活动中:

 final Context context = getActivity(); // looks like class field initialization

直接调用它:MyFragmentClass.this.getActivity()

答案 1 :(得分:0)

您应该在onActivityCreated内获取活动参考。

 @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
                    context = getActivity();
    }

一旦调用此函数,活动仅在Fragment内可用。