Android 来自 GCM 的下游消息 "no empty constructor" 错误
Android downstream message from GCM "no empty constructor" error
我正在从 php 脚本向 Android 应用程序发送下游消息。但是接收 Android 应用程序崩溃了,我在 logcat 上得到了 "Caused by: java.lang.InstantiationException: can't instantiate class mypackage.myactivity$MyGcmListenerService; no empty constructor"。我有一个空的构造函数。这是接收方服务:
public class MyGcmListenerService extends GcmListenerService {
public static final String UPDATE_COORDINATES = "mypackage.newcoordinate";
LocalBroadcastManager coordintesupdater;
public MyGcmListenerService() { super(); }
@Override
public void onMessageSent(String msgId) {
Log.i("the damn message " + msgId, "sent");
super.onMessageSent(msgId);
}
@Override
public void onDeletedMessages() {
super.onDeletedMessages();
}
@Override
public void onSendError(String msgId, String error) {
super.onSendError(msgId, error);
}
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("lat") + ", " + data.getString("lon");
if (message != null && !message.isEmpty()) {
Intent mIntent = new Intent();
mIntent.putExtra(UPDATE_COORDINATES, message);
mIntent.setAction(UPDATE_COORDINATES); //should match the receiver intent filter at the registering
coordintesupdater.sendBroadcast(mIntent);
} else {
Log.i("Received", "empty message");
}
}
}
这是我的清单,其中明确声明了服务
<service
android:name=".myactivity$MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
我整个晚上都在研究这个问题几个小时,但我无法解决它。我认为这个例外很简单,只是表示有问题,但没有具体说明。有人可以指出我的问题吗?
基于此
mypackage.MapActivity$MyGcmListenerService
我假设 MyGcmListenerService
是嵌套的 class。但是它不是静态的,这意味着它需要 class MapActivity
的实例作为要创建的隐式参数。
如果您不从 MyGcmListenerService
访问 MapActivity
您可以通过声明后者 class static:
来解决问题
public static class MyGcmListenerService extends GcmListenerService
我正在从 php 脚本向 Android 应用程序发送下游消息。但是接收 Android 应用程序崩溃了,我在 logcat 上得到了 "Caused by: java.lang.InstantiationException: can't instantiate class mypackage.myactivity$MyGcmListenerService; no empty constructor"。我有一个空的构造函数。这是接收方服务:
public class MyGcmListenerService extends GcmListenerService {
public static final String UPDATE_COORDINATES = "mypackage.newcoordinate";
LocalBroadcastManager coordintesupdater;
public MyGcmListenerService() { super(); }
@Override
public void onMessageSent(String msgId) {
Log.i("the damn message " + msgId, "sent");
super.onMessageSent(msgId);
}
@Override
public void onDeletedMessages() {
super.onDeletedMessages();
}
@Override
public void onSendError(String msgId, String error) {
super.onSendError(msgId, error);
}
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("lat") + ", " + data.getString("lon");
if (message != null && !message.isEmpty()) {
Intent mIntent = new Intent();
mIntent.putExtra(UPDATE_COORDINATES, message);
mIntent.setAction(UPDATE_COORDINATES); //should match the receiver intent filter at the registering
coordintesupdater.sendBroadcast(mIntent);
} else {
Log.i("Received", "empty message");
}
}
}
这是我的清单,其中明确声明了服务
<service
android:name=".myactivity$MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
我整个晚上都在研究这个问题几个小时,但我无法解决它。我认为这个例外很简单,只是表示有问题,但没有具体说明。有人可以指出我的问题吗?
基于此
mypackage.MapActivity$MyGcmListenerService
我假设 MyGcmListenerService
是嵌套的 class。但是它不是静态的,这意味着它需要 class MapActivity
的实例作为要创建的隐式参数。
如果您不从 MyGcmListenerService
访问 MapActivity
您可以通过声明后者 class static:
public static class MyGcmListenerService extends GcmListenerService