空对象引用

时间:2017-01-10 05:01:50

标签: android

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: websters.smartindiaserviceapp, PID: 3320
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{websters.smartindiaserviceapp/websters.smartindiaserviceapp.Activities.WelcomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentManager.beginTransaction()' on a null object reference
                      at android.support.v4.app.DialogFragment.dismissInternal(DialogFragment.java:200)
                      at android.support.v4.app.DialogFragment.dismiss(DialogFragment.java:171)
                      at websters.smartindiaserviceapp.Activities.WelcomeActivity.onCreate(WelcomeActivity.java:51)
                      at android.app.Activity.performCreate(Activity.java:6237)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

当我尝试在actvtiy

中解除自定义对话框时,我得到了这个例外
public class CustomDialog extends DialogFragment {

   public static CustomDialog newInstance() {
        return new CustomDialog();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.dialog, container, false);
        Glide.with(this).load(R.drawable.loader).placeholder(R.drawable.loader)
                //.dontAnimate()
                .into((ImageView) v.findViewById(R.id.progress));
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        if (getDialog() == null)
            super.setShowsDialog (false);
        return v;
    }

}

这是我的自定义对话框类。

我正在使用此代码显示对话框: CustomDialog.newInstance().show(getSupportFragmentManager(),"Dialog");正常显示对话框

并尝试解雇 使用CustomDialog.newInstance().dismiss();此代码但在这里我得到了Exception,请告诉我在哪里做错了如何解雇给对话框。

3 个答案:

答案 0 :(得分:4)

newInstance每次都会给你一个新的参考

初始化对话框,如

CustomDialog customDialog = CustomDialog.newInstance(); 
customDialog.show(getSupportFragmentManager(),"Dialog");

解雇使用

customDialog.dismiss();

答案 1 :(得分:1)

问题是由:

引起的
CustomDialog.newInstance().dismiss();

因为首先使用Fragment的两个实例来显示Dialog,所以将其解雇。

使用由

返回的相同Fragment实例
CustomDialog dialogFrag=CustomDialog.newInstance();
dialogFrag.show(getSupportFragmentManager(),"Dialog");

解雇为:

dialogFrag.dismiss();

答案 2 :(得分:0)

似乎你再次调用newInstance()来解除它会返回一个新的对话实例。尝试使用你已经显示过的同一个实例,也可以解雇。