非活动结束活动 [英] finish activity from non activity

查看:123
本文介绍了非活动结束活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的应用程序。我想打电话给从1日的活动一个警告对话框。该警报对话框被写入另一个类这是一种非活动类。应用程序的需求是调用活动应该完成的这是写在非活性类警报。调用警报的code是如下。

调用从1日的活动警报:

 公共类活动1扩展活动{
    / **第一次创建活动时调用。 * /
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.list_layout);
    } //的onCreate结束。

        / **后退按钮。 * /
    @覆盖
    公共布尔的onkeydown(INT键code,KeyEvent的事件){
        如果(键code == KeyEvent.KEY code_BACK){
            字符串消息=是否要退出。
            Utility.callAlert(消息,getApplicationContext());
            返回true;
        }
        返回super.onKeyDown(键code,事件);
    }
}
 

和具有警告对话框被叫类:

 公共类工具{
    // callAlert方法来显示警报,当异常发生。
    公共静态无效callAlert(字符串消息,最终的上下文语境){
        AlertDialog.Builder alertDialogBu​​ilder =新AlertDialog.Builder(上下文);
        alertDialogBu​​ilder.setTitle(MyApplication的。);
        alertDialogBu​​ilder.setMessage(消息);
        alertDialogBu​​ilder.setNeutralButton(OK,新DialogInterface.OnClickListener(){

            公共无效的onClick(DialogInterface对话,诠释它){
                ((活性1)context.getApplicationContext())完成()。
            } //结束的onClick。
        }); //结束alertDialog.setButton。
        alertDialogBu​​ilder.show();
    }
}
 

但是,这给我的错误是:

  02-25 17:00:45.961:D / AndroidRuntime(444):关闭虚拟机
02-25 17:00:45.961:W / dalvikvm(444):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40015560)
02-25 17:00:45.971:E / AndroidRuntime(444):致命异常:主要
02-25 17:00:45.971:E / AndroidRuntime(444):android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不是一个应用程序
02-25 17:00:45.971:E / AndroidRuntime(444):在android.view.ViewRoot.setView(ViewRoot.java:531)
02-25 17:00:45.971:E / AndroidRuntime(444):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
02-25 17:00:45.971:E / AndroidRuntime(444):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-25 17:00:45.971:E / AndroidRuntime(444):在android.app.Dialog.show(Dialog.java:241)
02-25 17:00:45.971:E / AndroidRuntime(444):在android.app.AlertDialog $ Builder.show(AlertDialog.java:802)
02-25 17:00:45.971:E / AndroidRuntime(444):在com.xxx.myapp.specialclasses.Utility.callAlert(Utility.java:25)
02-25 17:00:45.971:E / AndroidRuntime(444):在com.xxx.myapp.activities.Activity1.onKeyDown(Activity1.java:149)
02-25 17:00:45.971:E / AndroidRuntime(444):在android.view.KeyEvent.dispatch(KeyEvent.java:1256)
02-25 17:00:45.971:E / AndroidRuntime(444):在android.app.Activity.dispatchKeyEvent(Activity.java:2078)
02-25 17:00:45.971:E / AndroidRuntime(444):在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchKeyEvent(PhoneWindow.java:1663)
02-25 17:00:45.971:E / AndroidRuntime(444):在android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560)
02-25 17:00:45.971:E / AndroidRuntime(444):在android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535)
02-25 17:00:45.971:E / AndroidRuntime(444):在android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
02-25 17:00:45.971:E / AndroidRuntime(444):在android.os.Handler.dispatchMessage(Handler.java:99)
02-25 17:00:45.971:E / AndroidRuntime(444):在android.os.Looper.loop(Looper.java:123)
02-25 17:00:45.971:E / AndroidRuntime(444):在android.app.ActivityThread.main(ActivityThread.java:3683)
02-25 17:00:45.971:E / AndroidRuntime(444):在java.lang.reflect.Method.invokeNative(本机方法)
02-25 17:00:45.971:E / AndroidRuntime(444):在java.lang.reflect.Method.invoke(Method.java:507)
02-25 17:00:45.971:E / AndroidRuntime(444):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
02-25 17:00:45.971:E / AndroidRuntime(444):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-25 17:00:45.971:E / AndroidRuntime(444):在dalvik.system.NativeStart.main(本机方法)
02-25 17:05:46.0​​61:I /处理(444):发送信号。 PID:444 SIG:9
 

解决方案

来代替:

  Utility.callAlert(消息,getApplicationContext());
 

使用:

  Utility.callAlert(消息,Activity1.this);
 

 公共无效的onClick(DialogInterface对话,诠释它){
                ((活动)上下文).finish();
}
 

I am developing an app. I want to call a alert dialog from 1st activity. The alert dialog is written in another class which is a non activity class. The need of application is that the calling activity should finish from alert which is written in non activity class. The code of calling the alert is as below.

Calling the alert from 1st activity:

public class Activity1 extends Activity {
    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_layout);
    }// End of onCreate.

        /** Back Button. */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            String message = "Do you Want to Exit.";
            Utility.callAlert(message, getApplicationContext());
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }   
}

And the called class having alert dialog is :

public class Utility {
    // callAlert method to display alert, when exception occur.
    public static void callAlert(String message, final Context context){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder.setTitle("MyApplication.");
        alertDialogBuilder.setMessage(message);
        alertDialogBuilder.setNeutralButton("Ok", new DialogInterface.OnClickListener() {                   

            public void onClick(DialogInterface dialog, int which) {
                ((Activity1)context.getApplicationContext()).finish();
            } //end onClick.
        }); // end alertDialog.setButton.
        alertDialogBuilder.show();  
    }
}

But this gives me the error as :

02-25 17:00:45.961: D/AndroidRuntime(444): Shutting down VM
02-25 17:00:45.961: W/dalvikvm(444): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-25 17:00:45.971: E/AndroidRuntime(444): FATAL EXCEPTION: main
02-25 17:00:45.971: E/AndroidRuntime(444): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
02-25 17:00:45.971: E/AndroidRuntime(444):  at android.view.ViewRoot.setView(ViewRoot.java:531)
02-25 17:00:45.971: E/AndroidRuntime(444):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
02-25 17:00:45.971: E/AndroidRuntime(444):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-25 17:00:45.971: E/AndroidRuntime(444):  at android.app.Dialog.show(Dialog.java:241)
02-25 17:00:45.971: E/AndroidRuntime(444):  at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
02-25 17:00:45.971: E/AndroidRuntime(444):  at com.xxx.myapp.specialclasses.Utility.callAlert(Utility.java:25)
02-25 17:00:45.971: E/AndroidRuntime(444):  at com.xxx.myapp.activities.Activity1.onKeyDown(Activity1.java:149)
02-25 17:00:45.971: E/AndroidRuntime(444):  at android.view.KeyEvent.dispatch(KeyEvent.java:1256)
02-25 17:00:45.971: E/AndroidRuntime(444):  at android.app.Activity.dispatchKeyEvent(Activity.java:2078)
02-25 17:00:45.971: E/AndroidRuntime(444):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663)
02-25 17:00:45.971: E/AndroidRuntime(444):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560)
02-25 17:00:45.971: E/AndroidRuntime(444):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535)
02-25 17:00:45.971: E/AndroidRuntime(444):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
02-25 17:00:45.971: E/AndroidRuntime(444):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-25 17:00:45.971: E/AndroidRuntime(444):  at android.os.Looper.loop(Looper.java:123)
02-25 17:00:45.971: E/AndroidRuntime(444):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-25 17:00:45.971: E/AndroidRuntime(444):  at java.lang.reflect.Method.invokeNative(Native Method)
02-25 17:00:45.971: E/AndroidRuntime(444):  at java.lang.reflect.Method.invoke(Method.java:507)
02-25 17:00:45.971: E/AndroidRuntime(444):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-25 17:00:45.971: E/AndroidRuntime(444):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-25 17:00:45.971: E/AndroidRuntime(444):  at dalvik.system.NativeStart.main(Native Method)
02-25 17:05:46.061: I/Process(444): Sending signal. PID: 444 SIG: 9

解决方案

instead of:

Utility.callAlert(message, getApplicationContext());

use:

Utility.callAlert(message, Activity1.this);

and

public void onClick(DialogInterface dialog, int which) {
                ((Activity)context).finish();
}

这篇关于非活动结束活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆