Android - 使用全局异常时无法单击按钮

时间:2015-07-01 03:59:25

标签: android exception exception-handling

我在主要活动中使用全局异常处理程序,并且我有一些按钮。如果调用未捕获的异常,则onclicklistener按钮不起作用。

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
          @Override
          public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
              Log.e("Alert","Lets See if it Works !!!");

          }
      });
     setContentView(R.layout.activity_graphical_report);

    Button samBtn = findViewById(R.id.imggraphview);

    samBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(MainActivity.this, SecondActivity.class)
            startActivity(myIntent);
        }
    });

}

错误:

06-30 16:31:20.791: E/AndroidRuntime(2324): FATAL EXCEPTION: main
06-30 16:31:20.791: E/AndroidRuntime(2324): java.lang.IllegalArgumentException: View not attached to window manager
06-30 16:31:20.791: E/AndroidRuntime(2324):     at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:672)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:368)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:160)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at android.app.Dialog.dismissDialog(Dialog.java:319)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at android.app.Dialog.dismiss(Dialog.java:302)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at in.co.amiindia.vitalsservice.MessageTask.onPostExecute(SourceFile:73)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at in.co.amiindia.vitalsservice.MessageTask.onPostExecute(SourceFile:1)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at android.os.AsyncTask.finish(AsyncTask.java:631)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at android.os.Looper.loop(Looper.java:137)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at android.app.ActivityThread.main(ActivityThread.java:4895)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at java.lang.reflect.Method.invokeNative(Native Method)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at java.lang.reflect.Method.invoke(Method.java:511)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
06-30 16:31:20.791: E/AndroidRuntime(2324):     at dalvik.system.NativeStart.main(Native Method)

任何人都可以帮我解决这个问题。

提前致谢

2 个答案:

答案 0 :(得分:0)

首先设置您的视图。

setContentView(R.layout.activity_graphical_report);
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
          @Override
          public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
              Log.e("Alert","Lets See if it Works !!!");

          }
      });

您的按钮不是类型转换。

答案 1 :(得分:0)

投射可能有问题的按钮

Button samBtn = (Button)findViewById(R.id.imggraphview);