很抱歉,如果这是一个重复的问题,它看起来很基本,但我找不到任何答案。 我正在尝试在Kindle Fire平板电脑上运行亚马逊的SNS KindleMobilePushApp演示应用程序,但是我收到以下错误:
10-09 14:19:33.508 3101-3101/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40b05228)
10-09 14:19:33.508 3101-3101/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate service com.amazonaws.kindletest.ADMMessageHandler: java.lang.RuntimeException: Stub!
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2315)
at android.app.ActivityThread.access$1600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1231)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4533)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Stub!
at com.amazon.device.messaging.ADMMessageHandlerBase.<init>(Unknown Source)
at com.amazonaws.kindletest.ADMMessageHandler.<init>(ADMMessageHandler.java:32)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2312)
at android.app.ActivityThread.access$1600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1231)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4533)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
10-09 14:21:08.359 3101-3101/? E/AndroidRuntime﹕ Handle UnCaght exceptions. KILLING PID: 3101
这发生在示例项目的此类的默认构造函数中:
package com.amazonaws.kindletest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.amazon.device.messaging.ADMConstants;
import com.amazon.device.messaging.ADMMessageHandlerBase;
import com.amazon.device.messaging.ADMMessageReceiver;
public class ADMMessageHandler extends ADMMessageHandlerBase {
private static String savedMessage = null;
private static int numberOfMissedMessages = 0;
public static boolean inBackground = true;
public ADMMessageHandler(){
super(ADMMessageHandler.class.getName()); //ERROR!
}
...
}
这是从亚马逊图书馆的这个类派生的:
package com.amazon.device.messaging.ADMMessageHandlerBase
import android.app.IntentService;
import android.content.Intent;
public abstract class ADMMessageHandlerBase extends IntentService {
public ADMMessageHandlerBase(String var1) {
super((String)null);
throw new RuntimeException("Stub!");
}
protected final void onHandleIntent(Intent var1) {
throw new RuntimeException("Stub!");
}
protected abstract void onMessage(Intent var1);
protected abstract void onRegistrationError(String var1);
protected abstract void onRegistered(String var1);
protected abstract void onUnregistered(String var1);
}
我知道亚马逊的类是一个旨在被实际实现取代的存根,但是如何做到这一点?这是我的build.gradle:
buildscript {
repositories {
mavenCentral() // or jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1+'
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 18
buildToolsVersion "23.0.1"
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
defaultConfig {
applicationId "com.amazonaws.kindletest"
minSdkVersion 11
targetSdkVersion 18
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
并表明:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:amazon="http://schemas.amazon.com/apk/res/android"
package="com.amazonaws.kindletest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<!-- This permission ensures that no other application can intercept your ADM messages. It
should have the form packagename.permission.RECIEVE_ADM_MESSAGE where packagename is the
name defined in the "package" property of the manifest tag. -->
<permission android:name="com.amazonaws.kindletest.permission.RECEIVE_ADM_MESSAGE"
android:protectionLevel="signature"/>
<!-- Required permissions -->
<uses-permission android:name="com.amazonaws.kindletest.permission.RECEIVE_ADM_MESSAGE"/>
<uses-permission android:name="com.amazon.device.messaging.permission.RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.amazonaws.kindletest.KindleMobilePushApp"
android:screenOrientation="portrait"
android:label="@string/title_activity_main"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Your application's API Key -->
<meta-data android:name="AmazonAPIKey" android:value="@string/api_key"/>
<!-- Declare your ADMMessageHandlerBase implementation as a service -->
<service android:name="com.amazonaws.kindletest.ADMMessageHandler"
android:exported="false" />
<!-- You must explicitly enable ADM. You must also declare whether your application will run with or without ADM.
If you specify android:required="false", your app must degrade gracefully when ADM is unavailable. -->
<amazon:enable-feature android:name="com.amazon.device.messaging"
android:required="true" />
<receiver android:name="com.amazonaws.kindletest.ADMMessageHandler$MessageAlertReceiver"
android:permission="com.amazon.device.messaging.permission.SEND">
<intent-filter>
<action android:name="com.amazon.device.messaging.intent.REGISTRATION" />
<action android:name="com.amazon.device.messaging.intent.RECEIVE" />
<category android:name="com.amazonaws.kindletest"/>
</intent-filter>
</receiver>
</application>
</manifest>
我在亚马逊的开发者论坛上发布了posted about this但尚未成功。如果您需要更多信息,请告诉我,谢谢。
答案 0 :(得分:2)
以下是您遇到问题的原因:
您需要本地amazon-device-messaging.jar文件来编译您的应用程序,但是您不需要在运行时包含它,因为亚马逊设备将具有必要的类和方法。
以下是解决方案:
而不是
compile files('libs/amazon-device-messaging-1.0.1.jar')
你必须使用
provided files('libs/amazon-device-messaging-1.0.1.jar')
在您的情况下:您可以通过以下方式替换您的依赖项:
...
dependencies {
compile fileTree(dir: 'libs', include: '*.jar', exclude: 'amazon-device-messaging-1.0.1.jar')
provided files('libs/amazon-device-messaging-1.0.1.jar')
}
...
重复的答案:Android Studio runtime error Stub! at com.amazon.device.messaging.ADMMessageReceiver.<init>()
答案 1 :(得分:0)
对于我来说,下一个解决方案对我有所帮助: 放入清单
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
更多信息:https://issuetracker.google.com/issues/79478779
希望它可以帮助某人! )