2011-06-19 15 views
1

トーストやダイアログのように私がよく使ういくつかのものでクラスを作成しました。私はこのクラスの名前をMyUtilsと命名しました。私は内部に次のコード行を持っていた。MyUtils subYesNoDialogResultは、boolean型のプライベート変数です。別のオブジェクトを使用してAlertDialogを表示する方法

public void subYesNoDialog(Context appctx,String title,String mymessage) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(appctx); 
    builder.setMessage(mymessage) 
    .setTitle(title) 
    .setCancelable(false) 
    .setPositiveButton(android.R.string.yes, 
     new DialogInterface.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int id) { 
      subYesNoDialogResult = true; 
     } 
    }) 
    .setNegativeButton(android.R.string.no, 
     new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      subYesNoDialogResult = false; 
      dialog.cancel(); 
     } 
    }); 
    AlertDialog alert = builder.create();  
    alert.show(); 
} 

私は別のクラスを使用していますが、これは特定の目的のために主なアクティビティクラスを助けます。私はこのクラスをMyDbHelperと呼ぶ。上記の関数を呼び出して、はい/いいえダイアログを表示しようとしました。これは具体的には、MyDbHelperクラス内で何らかの処理を行う前に、ユーザーの入力を求めることです。しかし、私はこれを行う際にNullPointerExceptionを取得します。ここに私のlogcatを入れている。

I/ActivityManager( 67): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.xxx.geolog/.GeologActivity } 
D/AndroidRuntime( 676): Shutting down VM 
D/dalvikvm( 676): Debugger has detached; object registry had 1 entries 
I/ActivityManager( 67): Start proc com.xxx.geolog for activity com.xxx.geolog/.GeologActivity: pid=683 uid=10040 gids={} 
I/AndroidRuntime( 676): NOTE: attach of thread 'Binder Thread #3' failed 
D/dalvikvm( 33): GC_EXPLICIT freed 244 objects/9464 bytes in 704ms 
D/dalvikvm( 33): GC_EXPLICIT freed 2 objects/64 bytes in 350ms 
D/dalvikvm( 33): GC_EXPLICIT freed 2 objects/48 bytes in 536ms 
W/GpsLocationProvider( 67): Duplicate add listener for uid 10040 
W/WindowManager( 67): Attempted to add window with non-application token WindowToken{44f7d2a0 token=null}. Aborting. 
D/AndroidRuntime( 683): Shutting down VM 
W/dalvikvm( 683): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
E/AndroidRuntime( 683): FATAL EXCEPTION: main 
E/AndroidRuntime( 683): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
E/AndroidRuntime( 683): at android.view.ViewRoot.setView(ViewRoot.java:509) 
E/AndroidRuntime( 683): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 
E/AndroidRuntime( 683): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
E/AndroidRuntime( 683): at android.app.Dialog.show(Dialog.java:241) 
E/AndroidRuntime( 683): at com.xxx.geolog.SubLocationListener.onProviderDisabled(SubLocationListener.java:33) 
E/AndroidRuntime( 683): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:204) 
E/AndroidRuntime( 683): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:124) 
E/AndroidRuntime( 683): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:140) 
E/AndroidRuntime( 683): at android.os.Handler.dispatchMessage(Handler.java:99) 
E/AndroidRuntime( 683): at android.os.Looper.loop(Looper.java:123) 
E/AndroidRuntime( 683): at android.app.ActivityThread.main(ActivityThread.java:4627) 
E/AndroidRuntime( 683): at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime( 683): at java.lang.reflect.Method.invoke(Method.java:521) 
E/AndroidRuntime( 683): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
E/AndroidRuntime( 683): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
E/AndroidRuntime( 683): at dalvik.system.NativeStart.main(Native Method) 
W/ActivityManager( 67): Force finishing activity com.xxx.geolog/.GeologActivity 
W/ActivityManager( 67): Activity pause timeout for HistoryRecord{45030820 com.xxx.geolog/.GeologActivity} 

どうすればこの問題を解決できますか?私は間違ったことをしましたか?

答えて

1

ダイアログを表示するためにアプリケーションコンテキストを使用しようとしていますか? そうであれば、代わりにアクティビティ・コンテキストを使用して違いが生じるかどうかを確認してください。

0

あなたはこのメソッドに引数Context appctxを渡していないようです。

1

実際には別の点があります。 mibollmaが言ったように、実際にはGUIを提供するこのアプリケーションの唯一のアクティビティであるアクティビティコンテキストです。私はそれを取得し、型キャストをアクティビティに適用し、所有者アクティビティを明示的に設定します。ここでのポイントは、すべてのダイアログに所有者が必要だということです。それらがアクティビティクラスの内部で使用される場合、親アクティビティは暗黙の所有者になります。彼らはActivityクラス外で使用している場合や、あなただけは、問題を修正しました() alert.showの上に、私の場合はそうdialog_instance.setOwnerActivity(Activity owner);

この1行を必要とします!ご支援のための

alert.setOwnerActivity((Activity)appctx);

おかげ

関連する問題