我已经从AWS Mobile Hub下载了一个示例项目,该项目集成了SNS推送服务。
在我现有的应用程序中,我将com.amazonaws.mobile
包复制到了我的应用程序java
目录中。此外,我已将AWS gradle库添加到模块的gradle文件中,并将meta-data
和receiver
属性添加到清单文件中。
在我的MainActivity.java
中,我有以下代码:
import com.amazonaws.mobile.AWSMobileClient;
import com.amazonaws.mobile.push.PushManager;
private PushManager pushManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
pushManager = AWSMobileClient.defaultMobileClient().getPushManager();
toggleNotification(true);
}
toggleNotification
方法来自演示应用程序。问题部分是我初始化pushManager
(pushManager = AWSMobileClient...
)的行。这是日志:E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.intap.snspushtest, PID: 25796
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.intap.snspushtest/com.intap.snspushtest.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.amazonaws.mobile.push.PushManager com.amazonaws.mobile.AWSMobileClient.getPushManager()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.amazonaws.mobile.push.PushManager com.amazonaws.mobile.AWSMobileClient.getPushManager()' on a null object reference
at com.intap.snspushtest.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
它说
getPushManager()
是一个空对象引用,这很奇怪,因为在示例应用程序中它很好用,而且我对复制的文件没有做任何更改...你能帮我吗?我仍在尝试找出为什么它为空...
最佳答案
原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'com.amazonaws.mobile.push.PushManager com.amazonaws.mobile.AWSMobileClient.getPushManager()'
这表明AWSMobileClient.defaultMobileClient()返回null,因此无法调用getPushManager()。必须先调用AWSMobileClient.initializeMobileClientIfNecessary(context)才能初始化移动客户端,然后再调用AWSMobileClient.defaultMobileClient()。初始化通常在Application类中执行,如下所示:
import android.support.multidex.MultiDexApplication;
import android.util.Log;
import com.amazonaws.mobile.AWSMobileClient;
/**
* Application class responsible for initializing singletons and other common components.
*/
public class Application extends MultiDexApplication {
private final static String LOG_TAG = Application.class.getSimpleName();
@Override
public void onCreate() {
Log.d(LOG_TAG, "Application.onCreate - Initializing application...");
super.onCreate();
initializeApplication();
Log.d(LOG_TAG, "Application.onCreate - Application initialized OK");
}
private void initializeApplication() {
// Initialize the AWS Mobile Client
AWSMobileClient.initializeMobileClientIfNecessary(getApplicationContext());
// ... Put any application-specific initialization logic here ...
}
}