Android应用程序在一段时间后崩溃了

时间:2014-01-22 02:26:54

标签: java android timer

我遇到了一个问题,我对导致崩溃的原因一无所知。我有一个内部类的Timer,它每分钟从MainActivity onCreate()触发。

timer = new Timer(); 
timer.scheduleAtFixedRate(new RemindTask(),2000, 10000);

这是我的Timer类。这个计时器类负责启动另一个活动。

class RemindTask extends TimerTask {

public CaptureActivityHandler cah = new CaptureActivityHandler();
public void run() {
    Log.d("TIMER",  "timer is triggered");
    TimerCount++;

    if(TimerCount > 1)
    {
        Log.d("TIMER",  "TimerCount > 1");
        boolean flag = cah.getDecodeFlag();

        if(flag){
            Log.d("TIMER",  "decodeFlag true");
            cah.setDecodeFlag(false);
            Intent intent = new Intent(MainActivity.this,CaptureActivity.class);
            startActivityForResult(intent,SCAN_QRCODE_REQUEST);
        }
        else
        {
            Intent intent = new Intent(MainActivity.this,CaptureActivity.class);
            startActivityForResult(intent,SCAN_QRCODE_REQUEST);
        }
    }
    else {
        Intent intent = new Intent(MainActivity.this,CaptureActivity.class);
        startActivityForResult(intent,SCAN_QRCODE_REQUEST);
    }
   }
}

问题是应用程序在15小时后崩溃了。 Logcat显示:

01-21 11:01:42.027: W/dalvikvm(30401): threadid=11: thread exiting with uncaught  exception (group=0x41ddd930)
01-21 11:01:42.027: W/ActivityManager(363): Unable to find app for caller   android.app.ApplicationThreadProxy@4275bf18 (pid=-1) when starting: Intent {  cmp=com.handalindah.manjalinkkiosk/tw.com.quickmark.sdk.CaptureActivity }
01-21 11:01:42.027: E/AndroidRuntime(30401): FATAL EXCEPTION: Timer-0
01-21 11:01:42.027: E/AndroidRuntime(30401): java.lang.SecurityException: Not allowed to start activity Intent { cmp=com.handalindah.manjalinkkiosk/tw.com.quickmark.sdk.CaptureActivity }
01-21 11:01:42.027: E/AndroidRuntime(30401):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1625)
01-21 11:01:42.027: E/AndroidRuntime(30401):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
01-21 11:01:42.027: E/AndroidRuntime(30401):    at android.app.Activity.startActivityForResult(Activity.java:3370)
01-21 11:01:42.027: E/AndroidRuntime(30401):    at android.app.Activity.startActivityForResult(Activity.java:3331)
01-21 11:01:42.027: E/AndroidRuntime(30401):    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:839)
01-21 11:01:42.027: E/AndroidRuntime(30401):    at com.handalindah.manjalinkkiosk.MainActivity$RemindTask.run(MainActivity.java:562)
01-21 11:01:42.027: E/AndroidRuntime(30401):    at java.util.Timer$TimerImpl.run(Timer.java:284)
01-21 11:01:42.037: W/ActivityManager(363): Can't find mystery application for Crash from pid=30401 uid=10079: android.os.BinderProxy@42976028
01-21 11:01:42.037: I/Process(30401): Sending signal. PID: 30401 SIG: 9
01-21 11:01:42.147: W/InputDispatcher(363): channel '42600fd8 com.handalindah.manjalinkkiosk/com.handalindah.manjalinkkiosk.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
01-21 11:01:42.147: E/InputDispatcher(363): channel '42600fd8 com.handalindah.manjalinkkiosk/com.handalindah.manjalinkkiosk.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
01-21 11:01:42.147: W/InputDispatcher(363): Attempted to unregister already unregistered input channel '42600fd8 com.handalindah.manjalinkkiosk/com.handalindah.manjalinkkiosk.MainActivity (server)'
01-21 11:01:42.147: I/WindowState(363): WIN DEATH: Window{42600fd8 u0 com.handalindah.manjalinkkiosk/com.handalindah.manjalinkkiosk.MainActivity}

然后考虑到它可能由内存泄漏引起,那么我将计时器周期缩短到10秒。正如预期的那样,它在2.5小时后坠毁。

然而,由于使用的堆大小总是大约58%,因此内存似乎不太可能。此外,内存使用率约为4%,占设备内存总量的35%。

这是清单。

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

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

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.front" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:largeHeap="true"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.test.MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop">
       <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="tw.com.quickmark.sdk.CaptureActivity"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
        android:launchMode="singleTop">

    </activity>
    <activity android:name="tw.com.quickmark.sdk.PreferencesActivity" >
    </activity>

    <service
        android:name="com.example.test.MySpiceService"
        android:exported="false" >
    </service>
 </application>
</manifest>

有什么想法会导致崩溃吗?

2 个答案:

答案 0 :(得分:0)

不是实际答案,但您应将run()方法更改为此...

public void run() {
    Log.d("TIMER",  "timer is triggered");
    TimerCount++;

    if (TimerCount > 1) {
        Log.d("TIMER",  "TimerCount > 1");
        boolean flag = cah.getDecodeFlag();

        if(flag){
            Log.d("TIMER",  "decodeFlag true");
            cah.setDecodeFlag(false);
        }
    }

    Intent intent = new Intent(MainActivity.this,CaptureActivity.class);
            startActivityForResult(intent,SCAN_QRCODE_REQUEST);

}

除了少重复之外,它完全相同。

答案 1 :(得分:0)

异常显示“java.lang.SecurityException:不允许启动活动Intent {cmp = com.handalindah.manjalinkkiosk / tw.com.quickmark.sdk.CaptureActivity}”。

tw.com.quickmark.sdk.CaptureActivity在某些运行后未注册?