我遇到了一個我正在創建的應用程序的問題。基本上,應用程序會在您第一次嘗試打開它時崩潰,然後它就會正常運行。令人困惑的部分只有當您從Google Play下載應用時纔會發生。如果我直接從Android Studio將應用加載到手機上,則不會收到任何錯誤。RuntimeException僅在生產版本中加載應用程序
java.lang.RuntimeException:
at android.app.ActivityThread.handleReceiver (ActivityThread.java:3290)
at android.app.ActivityThread.-wrap20 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1715)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6682)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1410)
Caused by: java.lang.ClassNotFoundException:
at dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass (ClassLoader.java:380)
at java.lang.ClassLoader.loadClass (ClassLoader.java:312)
at android.app.ActivityThread.handleReceiver (ActivityThread.java:3285)
在研究了這個問題之後,我發現有人說要清除設備的緩存。這解決了這個問題......但這只是抹去了應用程序,並從那時起工作正常。初始安裝仍然崩潰。這發生在多個設備上,但我無法在調試時重現它。我唯一的想法是,在我調用setContentView()之後,我在onCreate()中有這個代碼。
Intent intent = new Intent(this, NotificationListener.class);
startService(intent);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
NotificationListener.class extends NotificationListenerService。除了最初的發佈之外,一切都很好。
UPDATE
這裏是我設置的AndroidManifest服務:
<service android:name=".NotificationListener"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
這裏是我的NotificationListenerService的簡化版本:
public class NotificationListener extends NotificationListenerService
{
private IBinder mBinder = new LocalBinder();
@Override
public void onNotificationPosted(StatusBarNotification sbn)
{
super.onNotificationPosted(sbn);
// custom code
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn)
{
super.onNotificationRemoved(sbn);
}
@Override
public IBinder onBind(Intent intent)
{
if (SERVICE_INTERFACE.equals(intent.getAction()))
return super.onBind(intent);
else return mBinder;
}
public class LocalBinder extends Binder
{
public NotificationListener getService()
{
return NotificationListener.this;
}
}
}
下面是ServiceConnection粘結劑:
private ServiceConnection serviceConnection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder)
{
NotificationListener.LocalBinder localBinder = (NotificationListener.LocalBinder) iBinder;
notificationListener = localBinder.getService();
bound = true;
// code to call custom function in NotificationListener class
}
@Override
public void onServiceDisconnected(ComponentName componentName)
{
notificationListener = null;
bound = false;
}
};
我知道這必須是服務綁定的問題,因爲如果我刪除所有的綁定代碼並啓動服務,則所有事情都按照它應該運行。只有當我添加綁定代碼,我得到這個錯誤。
你可以嘗試創建沒有proguard的apk文件嗎? – kimkevin
@KimKevin這是否只涉及刪除條目在應用程序gradle中的buildTypes?如果是這樣,我刪除它,並上傳了我的應用程序的測試版本,並仍然在啓動時出現相同的錯誤 – saboehnke
是的,它是minifyEnabled設置爲false或因爲發生ClassNotFoundException ...你註冊了嗎? NotificationListenerService到AndroidMani fest.xml? – kimkevin