2013-02-25 181 views
3

我正在開發一個應用程序。我想從第一個活動中調用一個警告對話框。警報對話框被寫入另一個非活動類的類中。應用程序的需要是調用活動應該從寫在非活動類中的警報完成。調用警報的代碼如下。從非活動完成活動

調用從1日的活動警報:

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); 
    } 
} 

,並具有警告對話框被叫類:

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(); 
    } 
} 

但是,這給我的錯誤是:

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 

回答

6

代替:

Utility.callAlert(message, getApplicationContext()); 

使用:

Utility.callAlert(message, Activity1.this); 

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

在活動,創建一個新班級

static public void finishThisActivity() { 
    finish(); 
} 

,並從對話框類稱之爲

YourActivityName.finishThisActivity(); 

,或者嘗試

YourActivityName.finish(); 
+0

這不起作用。你得到一個錯誤非靜態方法完成不能從靜態上下文引用 – 2016-02-12 03:10:25

1

試試這個:的

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

     public void onClick(DialogInterface dialog, int which) { 
      activity.finish(); 
     } //end onClick. 
    }); // end alertDialog.setButton. 
    alertDialogBuilder.show(); 
    } 
} 
+0

@Manoj Fegde工作嗎? – KunalK 2013-02-25 12:04:30

+0

沒有它沒有工作。由於它給出錯誤,因爲:無法對類型活動的非靜態方法typr()進行靜態引用。 – 2013-02-25 12:08:01

+0

同樣的錯誤? – KunalK 2013-02-25 12:10:02

0

試試這個,你是不是創造Dialog

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context).create(); 
0

我想我知道你的答案,因爲我已經做過一次自己。這是我的代碼,它運作良好!

((Activity)context)可用於調用您的Activity。

NonActivityClass nonActivityClass = new NonActivityClass(YourActivityClass.this); 
nonActivityClass.finishIt(); 

這是NonActivityClass。

public class NonActivityClass() { 
    private Context context; 

    public NonActivityClass(Context context) { 
     this.context=context; 
    } 

    public void finishIt() { 
     //Remember to import android.app.Activity; in this class! 
     ((Activity)context).finish(); //This destroy the your activity class 
    } 
} 

此外,它可以用來從非Activity類運行您的UI線程。例如,

((Activity)context).runOnUiThread() { 
    //Your stuff here! 
} 

希望它可以幫助你!