【问题标题】:Process dialog in service class in androidandroid中服务类中的进程对话框
【发布时间】:2014-04-16 11:24:00
【问题描述】:

我正在尝试在服务运行时间时运行进程对话框。我正在使用 hanler 在我的服务类中运行进程对话框。那个时候我有以下问题。请任何人帮助我.....

Logcat

04-16 16:43:57.691: W/dalvikvm(395): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-16 16:43:57.813: E/AndroidRuntime(395): FATAL EXCEPTION: main
04-16 16:43:57.813: E/AndroidRuntime(395): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.view.ViewRoot.setView(ViewRoot.java:531)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.Dialog.show(Dialog.java:241)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.ProgressDialog.show(ProgressDialog.java:107)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.ProgressDialog.show(ProgressDialog.java:95)
04-16 16:43:57.813: E/AndroidRuntime(395):  at com.appname.notification.StartNotification$1.handleMessage(StartNotification.java:235)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.os.Looper.loop(Looper.java:123)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-16 16:43:57.813: E/AndroidRuntime(395):  at java.lang.reflect.Method.invokeNative(Native Method)
04-16 16:43:57.813: E/AndroidRuntime(395):  at java.lang.reflect.Method.invoke(Method.java:507)
04-16 16:43:57.813: E/AndroidRuntime(395):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-16 16:43:57.813: E/AndroidRuntime(395):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-16 16:43:57.813: E/AndroidRuntime(395):  at dalvik.system.NativeStart.main(Native Method)
04-16 16:43:57.813: E/AndroidRuntime(395): FATAL EXCEPTION: main
04-16 16:43:57.813: E/AndroidRuntime(395): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.view.ViewRoot.setView(ViewRoot.java:531)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.Dialog.show(Dialog.java:241)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.ProgressDialog.show(ProgressDialog.java:107)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.ProgressDialog.show(ProgressDialog.java:95)
04-16 16:43:57.813: E/AndroidRuntime(395):  at com.appname.notification.StartNotification$1.handleMessage(StartNotification.java:235)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.os.Looper.loop(Looper.java:123)
04-16 16:43:57.813: E/AndroidRuntime(395):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-16 16:43:57.813: E/AndroidRuntime(395):  at java.lang.reflect.Method.invokeNative(Native Method)
04-16 16:43:57.813: E/AndroidRuntime(395):  at java.lang.reflect.Method.invoke(Method.java:507)
04-16 16:43:57.813: E/AndroidRuntime(395):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-16 16:43:57.813: E/AndroidRuntime(395):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-16 16:43:57.813: E/AndroidRuntime(395):  at dalvik.system.NativeStart.main(Native Method)

服务类。

public class StartNotification extends Service {

    SharedPreferences sharedPreferences;
    String notification_Url = KP.KPURL.NOTIFICATION;
    public ProgressDialog pDialog;
    public Message m = new Message();
    private static final int PROCESS_START = 0;
    private static final int PROCESS_STOP = 1;

    // "2014-02-04 00:00:00"
    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        processLoading();
        return super.onStartCommand(intent, flags, startId);
    }
public void storelocalDB() {
// my logic
}

    private void processLoading() {
        m.what = PROCESS_START;
        handler.sendMessage(m);
        storelocalDB();
        m = new Message();
        m.what = PROCESS_STOP;
        handler.sendMessage(m);
    }
private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case PROCESS_START:
                pDialog = ProgressDialog.show(getApplicationContext(),
                        " Please wait...!", "Loading...", true, false);
                break;
            case PROCESS_STOP:
                if (pDialog.isShowing()) {
                    pDialog.dismiss();
                }
                break;
            }
        }
    };
}

【问题讨论】:

    标签: android service handler


    【解决方案1】:

    尝试先创建一个新对象:

    ProgressDialog pdialog = new ProgressDialog(getActivity());
    

    我没有看到你创建了一个对象。

    【讨论】:

    • 现在我遇到了不同的问题“无法实例化服务 com.appname.notification.StartNotification: java.lang.NullPointerException”
    • 能否发布整个 logcat 异常和引发 NullPointerException 的行?
    • 你的代码 ProgressDialog pdialog = new ProgressDialog(getActivity());但我改变了 ProgressDialog pdialog = new ProgressDialog(getApplicationContext());.
    【解决方案2】:

    我不认为您可以从服务中显示 ProgressDialog。更好的选择是您必须从启动服务的位置显示活动对话框并从那里注册接收器。当服务停止时向活动发送广播并完成正在运行的进度对话框.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多