不幸的是,应用程序已停止工作,但几秒后运行

时间:2014-08-14 04:30:39

标签: android permissions

我正在开发不同医学测试的应用程序。然而,当我开始在模拟器上运行应用程序时,它显示“不幸的是,应用程序已停止工作 - >确定”但是几秒后它就会运行。每当我进入主屏幕时,它会显示上述消息。然而,在我按下确定后,应用程序按原样运行。 eclipse上没有显示任何错误或警告。我附上了清单文件。有人可以帮帮我吗?

清单文件如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mobility.ui"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 

        android:theme="@style/AppTheme" >
        <activity
            android:name="com.mobility.ui.MainActivity"
            android:screenOrientation="portrait"
            android:launchMode="singleInstance"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity android:name="com.mobility.ui.VTest"
            android:launchMode="singleInstance"
            android:screenOrientation="portrait"
            ></activity>

        <activity android:name="com.mobility.ui.ITest"
            android:launchMode="singleInstance"
            android:screenOrientation="portrait"
            ></activity>

        <activity android:name="com.mobility.ui.ATest"
            android:launchMode="singleInstance"
            android:screenOrientation="portrait"
            ></activity>

        <activity android:name="com.mobility.ui.Test"
            android:launchMode="singleInstance"
            android:screenOrientation="portrait"
            ></activity>

    </application>

</manifest>

logcat的: -

08-14 00:17:12.120: E/AndroidRuntime(1345):     at java.lang.reflect.Method.invokeNative(Native Method)
08-14 00:17:12.120: E/AndroidRuntime(1345):     at java.lang.reflect.Method.invoke(Method.java:515)
08-14 00:17:12.120: E/AndroidRuntime(1345):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-14 00:17:12.120: E/AndroidRuntime(1345):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-14 00:17:12.120: E/AndroidRuntime(1345):     at dalvik.system.NativeStart.main(Native Method)
08-14 00:17:12.120: E/AndroidRuntime(1345): Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.igate.mobility.visiontest.ui.UIScreenActivity$1@b2ce2338
08-14 00:17:12.120: E/AndroidRuntime(1345):     at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:667)
08-14 00:17:12.120: E/AndroidRuntime(1345):     at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1453)
08-14 00:17:12.120: E/AndroidRuntime(1345):     at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:489)
08-14 00:17:12.120: E/AndroidRuntime(1345):     at com.igate.mobility.visiontest.ui.UIScreenActivity.onPause(UIScreenActivity.java:55)
08-14 00:17:12.120: E/AndroidRuntime(1345):     at android.app.Activity.performPause(Activity.java:5335)
08-14 00:17:12.120: E/AndroidRuntime(1345):     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1233)
08-14 00:17:12.120: E/AndroidRuntime(1345):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3048)

UISCreenActivity.java文件:

private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
          Bundle bundle = intent.getExtras();
          if (bundle != null) {

//            String string = bundle.getString(DownloadService.FILEPATH);
//          int resultCode = bundle.getInt(DownloadService.RESULT);
//          
//          if (resultCode == RESULT_OK) {
//            
//              Toast.makeText(getApplicationContext(),"Download complete. Download URI: " + string,
//                Toast.LENGTH_LONG).show();
//            
////              textView.setText("Download done");
//          } else {
//            Toast.makeText(getApplicationContext(), "Download failed",Toast.LENGTH_LONG).show();
////              textView.setText("Download failed");
//          }
//          

          }
        }
      };


      @Override
      protected void onResume() {
        super.onResume();

//      registerReceiver(receiver, new IntentFilter(DownloadService.NOTIFICATION));

      }

      @Override
      protected void onPause() {
          if(receiver != null) {
            unregisterReceiver(receiver);
          }
        }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
//      overridePendingTransition(R.anim.anim_in,R.anim.anim_out);
    }

    public abstract void setObjects();

    // setting click listener to a view
    public void setOnClickListener(int[] viewIds){
        for (int i = 0; i < viewIds.length; i++) {
            ((View)findViewById(viewIds[i])).setOnClickListener(this);
        }
    }

    public void setInvisible(int viewId){
        ((View)findViewById(viewId)).setVisibility(View.INVISIBLE);
    }

}

2 个答案:

答案 0 :(得分:0)

onPause方法

中这样做
protected void onPause() {
  if(receiver != null) {
    unregisterReceiver(receiver);
  }
}

或者您可以使用try-catch阻止。

问题的根源在于:

  unregisterReceiver(receiver);

如果接收方已经取消注册(可能在您未在此帖中包含的代码中)或未注册,则调用unregisterReceiver会抛出IllegalArgumentException。在您的情况下,您需要为此异常添加特殊的try / catch并忽略它(假设您不能或不想控制在同一个接收器上调用unregisterReceiver的次数)。

答案 1 :(得分:0)

使用onCreate()方法注册您的接收器

registerReceiver(receiver, new IntentFilter("com.mobility.ui.receiverAction"));

取消注册onDestroy()而不是onPause()方法

@Override
      protected void onDestroy() {
          if(receiver != null) {
            unregisterReceiver(receiver);
          }
           super.onDestroy();
        }