onWindowFocusChanged()方法中的dismiss对话框崩溃应用程序

时间:2016-02-10 21:40:04

标签: android android-dialog

我有这样的活动

public class AboutActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);

        setItem(R.id.version, R.string.version, BuildConfig.VERSION_NAME);
        setButton(R.id.privacy_policy, R.string.privacy_policy, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPrivacy();
            }
        });
        setButton(R.id.open_source_licenses, R.string.open_source_licenses, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showOpenSourceLicenses();
            }
        });
    }

    private void setItem(int id, int labelRes, int value) {
        setItem(id, labelRes, Integer.valueOf(value).toString());
    }

    private void setItem(int id, int labelRes, String value) {
        View view = findViewById(id);
        TextView labelView = (TextView) view.findViewById(R.id.label);
        TextView valueView = (TextView) view.findViewById(R.id.value);

        labelView.setText(labelRes);
        valueView.setText(value);
    }

    private void setButton(int id, int labelRes, View.OnClickListener clickListener) {
        View view = findViewById(id);
        TextView labelView = (TextView) view.findViewById(R.id.label);
        TextView valueView = (TextView) view.findViewById(R.id.value);
        View navArrow = view.findViewById(R.id.nav_arrow);

        labelView.setText(labelRes);
        valueView.setVisibility(View.INVISIBLE);
        navArrow.setVisibility(View.VISIBLE);
        view.setOnClickListener(clickListener);
    }

    private void showPrivacy() {
        Intent intent = new Intent(this, PrivacyPolicyActivity.class);
        startActivity(intent);
    }

    private void showOpenSourceLicenses() {
        Intent intent = new Intent(this, OpenSourceLicenseActivity.class);
        startActivity(intent);
    }

    @Override
    public void onWindowFocusChanged (boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus && !WorkspaceApplication.isNetworkConnected(this)) {
            new NetworkLostConnectionDialog(this).show();
        }
    }
}

这是对话类

public class NetworkLostConnectionDialog extends Dialog {

    TextView descriptionView, headerView;
    Context context;

    public NetworkLostConnectionDialog(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onStart() {
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        Window window = getWindow();
        lp.copyFrom(window.getAttributes());
        //full width dialog and bottom position
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        setContentView(R.layout.error_dialog);
        descriptionView = (TextView) findViewById(R.id.error_description);
        headerView = (TextView) findViewById(R.id.error_header);
        headerView.setText(getContext().getResources().getString(R.string.no_network_error_header));
        descriptionView.setText(getContext().getResources().getString(R.string.no_network_error_description));
        setCancelable(false);
    }

    @Override
    public void onWindowFocusChanged (boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus) {
            if(MyApplication.isNetworkConnected(context)){
                dismiss();
            }
        }
    }
}

当活动可见并且我下拉通知菜单并关闭wifi时,当我返回活动时,会显示错误对话框,这是好的。 当显示错误对话框时,我重新打开wifi,焦点进入对话框并执行dismiss(),但随后它崩溃了应用程序。 这是堆栈跟踪

FATAL EXCEPTION: main
Process: com.myapplication.workspace, PID: 8020
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findFocus()' on a null object reference
    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3292)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

不知道是什么造成的。任何帮助表示赞赏。

0 个答案:

没有答案