0

我知道关于这个话题有很多答案......但我无法找到解决我的问题的方法。它很简单,一个活动:

public class DialogsActivity extends Activity {

Bundle bundle=new Bundle();
String token;

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    mostrarDialogo();
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dialogsactivitylayout);
    bundle=getIntent().getExtras();
    token=bundle.getString("token");
    token=token.replaceAll("\\s+","");
    Log.i("Parse", "Token: "+token);


}

private void mostrarDialogo() {
    if(token.equals("nologin")){
        AlertDialog.Builder nologin = new AlertDialog.Builder(DialogsActivity.this);
        nologin.setTitle(R.string.loginstring);
        nologin.setMessage(R.string.nologin);
        nologin.setNegativeButton(R.string.cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {

                        dialog.dismiss();
                        finish();
                        return;
                    }

                });


        nologin.show(); 
    }else if(token.equals("nocon")){
        AlertDialog.Builder nocon = new AlertDialog.Builder(DialogsActivity.this);
        nocon.setTitle(R.string.loginstring);
        nocon.setMessage(R.string.timeout);
        nocon.setNegativeButton(R.string.cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {

                        dialog.dismiss();
                        finish();
                        return;
                    }
                });
        nocon.show();

    }else if(token.equals("oklogin")){

        AlertDialog.Builder start=new AlertDialog.Builder(DialogsActivity.this);
        start.setTitle(R.string.warn);
        start.setMessage(R.string.service);
        start.setNegativeButton(R.string.accept,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                        finish();
                        return;
                    }
                });
        //finish();
        start.show();
    }else if(token!="nocon" && token!="nologin" && token!="oklogin"){
        AlertDialog.Builder error=new AlertDialog.Builder(DialogsActivity.this);
        error.setTitle(R.string.unknownerror);
        error.setMessage(R.string.unkerrortext);
        error.setPositiveButton(R.string.accept,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                        finish();
                        return;

                    }
                });
        error.show();
    }



}



}

当活动运行时,堆栈跟踪:

05-21 11:46:25.140: E/WindowManager(15737): Activity com.publidirecta.vinceriazafata.DialogsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405aeb60 that was originally added here
05-21 11:46:25.140: E/WindowManager(15737): android.view.WindowLeaked: Activity com.publidirecta.vinceriazafata.DialogsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405aeb60 that was originally added here
05-21 11:46:25.140: E/WindowManager(15737):     at android.view.ViewRoot.<init>(ViewRoot.java:263)
05-21 11:46:25.140: E/WindowManager(15737):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:171)
05-21 11:46:25.140: E/WindowManager(15737):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114)
05-21 11:46:25.140: E/WindowManager(15737):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.Dialog.show(Dialog.java:241)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.AlertDialog$Builder.show(AlertDialog.java:809)
05-21 11:46:25.140: E/WindowManager(15737):     at com.publidirecta.vinceriazafata.DialogsActivity.mostrarDialogo(DialogsActivity.java:88)
05-21 11:46:25.140: E/WindowManager(15737):     at com.publidirecta.vinceriazafata.DialogsActivity.onResume(DialogsActivity.java:20)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.Activity.performResume(Activity.java:3832)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2114)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-21 11:46:25.140: E/WindowManager(15737):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 11:46:25.140: E/WindowManager(15737):     at android.os.Looper.loop(Looper.java:130)
05-21 11:46:25.140: E/WindowManager(15737):     at android.app.ActivityThread.main(ActivityThread.java:3687)
05-21 11:46:25.140: E/WindowManager(15737):     at java.lang.reflect.Method.invokeNative(Native Method)
05-21 11:46:25.140: E/WindowManager(15737):     at java.lang.reflect.Method.invoke(Method.java:507)
05-21 11:46:25.140: E/WindowManager(15737):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-21 11:46:25.140: E/WindowManager(15737):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-21 11:46:25.140: E/WindowManager(15737):     at dalvik.system.NativeStart.main(Native Method)

错误总是在 .show() 方法上跳转,具体取决于它在一个 if 或另一个中输入的令牌变量的值。而且,如您所见,对话框总是被关闭。有什么帮助吗?谢谢!

4

3 回答 3

2

你必须使用

dialog.create().show();

我已经修改了您的代码以使其简单

private void mostrarDialogo() {
    if(token.equals("nologin")){
    showDialog(R.string.loginstring,R.string.nologin,R.string.cancel)

    }else if(token.equals("nocon")){
    showDialog(R.string.loginstring,R.string.timeout,R.string.cancel);       


    }else if(token.equals("oklogin")){
    showDialog(R.string.warn,R.string.service,R.string.accept);

    }else if(token!="nocon" && token!="nologin" && token!="oklogin"){
    showDialog(R.string.unknownerror,R.string.unkerrortext,R.string.accept);
   }

}

void showDialog(String title,String msg,String buttonText)
{
      AlertDialog.Builder dialog=new AlertDialog.Builder(DialogsActivity.this);
        dialog.setTitle(title);
        dialog.setMessage(msg);
        dialog.setPositiveButton(buttonText,
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    dialog.dismiss();
                    finish();
                    return;

                }
            });
    dialog.create().show();

}
于 2013-05-21T11:26:34.213 回答
1

您应该在显示之前创建对话框。

例如:

nocon.create().show();

反而

nocon.show();
于 2013-05-21T10:30:40.487 回答
-1

问题似乎Context与您正在创建的对象AlertDialog有关onResume()。每当您需要传递/使用Context时,始终建议尽可能使用getApplicationContext()object 而不是ActivityContext.

替换应该可以正常工作DialogsActivity.thisgetApplicationContext()希望这可以帮助。

于 2013-05-21T10:03:30.917 回答