我知道这个问题是重复的。但是我在这里找不到合适的解决方案。 我正在开发一个Android时钟小工具,并且我的Nexus 4和我兄弟的hTC One X设备上的所有东西都能顺利运行。但在其他设备上,Galaxy SII,SIII和Nexus 10一旦配置活动出现,应用程序崩溃,并显示强制停止消息,因为此行的NPE:
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
以下是包含该行的方法:
public void update(Context context, int nn){
final RemoteViews localRemoteViews = create(context);
prefsFile.getBoolean(String.valueOf(nn) + "app_onclick", true)) {
packageName = prefsFile.getString("packageName", "Not Available");
PackageManager packageManager = context.getPackageManager();
Intent i = new Intent(Intent.ACTION_MAIN, null);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i = packageManager.getLaunchIntentForPackage(packageName);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
localRemoteViews.setOnClickPendingIntent(R.id.img, pendingIntent);
}
widgetManager.updateAppWidget(nn, localRemoteViews);
}
的onReceive:
public void onReceive(Context context, Intent n)
{
super.onReceive(context, n);
prefsFile = context.getSharedPreferences("MyPrefs", 0);
ComponentName localComponentName = new ComponentName(context.getPackageName(), getClass().getName());
widgetManager = AppWidgetManager.getInstance(context);
WidgetIds = widgetManager.getAppWidgetIds(localComponentName);
if ((paramIntent.getAction().equals("android.intent.action.BOOT_COMPLETED")) && (this.mAppWidgetIds.length > 0))
{
for (int n : this.WidgetIds) {
packageName = prefsFile.getString("packageName", "Not Available");
update(context, n);
}
}
}
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.koshmen.clock.aimen"
android:versionCode="8"
android:versionName="1.3.4" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" >
<receiver
android:name="com.koshmen.clock.aimen.Yarab"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_stuff" />
</receiver>
<activity
android:name="com.koshmen.clock.aimen.YarabConfig"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
</application>
以下是我手机上安装的LogCat应用程序的Logcat结果,我无法在PC上使用它们,而上下文来自onReceive方法,该方法使用widgetID(n)传递上下文。 为什么应用程序在某些设备上运行而不在其他设备上?并给出NPE错误?
update method onReceive method
以下是Eclipse logCat的最新日志:
05-24 01:47:56.863: D/dalvikvm(23665): JIT code cache reset in 0 ms (0 bytes 4/0)
05-24 01:47:56.863: D/dalvikvm(23665): GC_FOR_ALLOC freed 481K, 41% free 9988K/16904K, paused 38ms, total 38ms
05-24 01:47:56.983: D/dalvikvm(23665): GC_FOR_ALLOC freed 502K, 41% free 9998K/16936K, paused 41ms, total 41ms
05-24 01:47:57.068: D/dalvikvm(23665): GC_FOR_ALLOC freed 225K, 41% free 10003K/16936K, paused 29ms, total 29ms
05-24 01:47:57.068: I/dalvikvm-heap(23665): Grow heap (frag case) to 17.453MB for 1468960-byte allocation
05-24 01:47:57.088: D/dalvikvm(23665): GC_FOR_ALLOC freed 0K, 38% free 11438K/18372K, paused 20ms, total 20ms
05-24 01:47:57.153: D/mali_winsys(23665): new_window_surface returns 0x3000
05-24 01:47:57.183: D/OpenGLRenderer(23665): Enabling debug mode 0
05-24 01:47:59.323: D/dalvikvm(23665): GC_FOR_ALLOC freed 11K, 38% free 11519K/18372K, paused 29ms, total 29ms
05-24 01:48:07.733: D/dalvikvm(23665): GC_FOR_ALLOC freed 45K, 38% free 11531K/18392K, paused 40ms, total 40ms
05-24 01:48:07.733: I/dalvikvm-heap(23665): Grow heap (frag case) to 17.721MB for 186640-byte allocation
05-24 01:48:07.768: D/dalvikvm(23665): GC_FOR_ALLOC freed <1K, 37% free 11713K/18576K, paused 36ms, total 36ms
05-24 01:48:07.808: D/dalvikvm(23665): GC_FOR_ALLOC freed <1K, 37% free 11713K/18576K, paused 38ms, total 38ms
05-24 01:48:07.808: I/dalvikvm-heap(23665): Grow heap (frag case) to 17.899MB for 186640-byte allocation
05-24 01:48:07.848: D/dalvikvm(23665): GC_FOR_ALLOC freed 0K, 37% free 11895K/18760K, paused 39ms, total 39ms
05-24 01:48:07.893: D/dalvikvm(23665): GC_FOR_ALLOC freed 0K, 37% free 12078K/18944K, paused 44ms, total 44ms
05-24 01:48:07.958: D/dalvikvm(23665): GC_FOR_ALLOC freed 1K, 36% free 12263K/19128K, paused 42ms, total 42ms
05-24 01:48:07.988: D/dalvikvm(23665): GC_FOR_ALLOC freed <1K, 36% free 12445K/19312K, paused 23ms, total 23ms
05-24 01:48:08.018: D/dalvikvm(23665): GC_FOR_ALLOC freed <1K, 35% free 12810K/19680K, paused 26ms, total 26ms
05-24 01:48:08.028: D/AndroidRuntime(23665): Shutting down VM
05-24 01:48:08.028: W/dalvikvm(23665): threadid=1: thread exiting with uncaught exception (group=0x415ebba8)
05-24 01:48:08.028: E/AndroidRuntime(23665): FATAL EXCEPTION: main
05-24 01:48:08.028: E/AndroidRuntime(23665): Process: com.koshmen.clock.aimen, PID: 23665
05-24 01:48:08.028: E/AndroidRuntime(23665): java.lang.RuntimeException: Unable to start receiver com.koshmen.clock.aimen.Yarab: java.lang.NullPointerException
05-24 01:48:08.028: E/AndroidRuntime(23665): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2426)
05-24 01:48:08.028: E/AndroidRuntime(23665): at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
05-24 01:48:08.028: E/AndroidRuntime(23665): at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
05-24 01:48:08.028: E/AndroidRuntime(23665): at android.app.ActivityThread.handleReceiver(Native Method)
05-24 01:48:08.028: E/AndroidRuntime(23665): at android.app.ActivityThread.access$1700(ActivityThread.java:135)
05-24 01:48:08.028: E/AndroidRuntime(23665): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
05-24 01:48:08.028: E/AndroidRuntime(23665): at android.os.Handler.dispatchMessage(Handler.java:102)
05-24 01:48:08.028: E/AndroidRuntime(23665): at android.os.Looper.loop(Looper.java:136)
05-24 01:48:08.028: E/AndroidRuntime(23665): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-24 01:48:08.028: E/AndroidRuntime(23665): at java.lang.reflect.Method.invokeNative(Native Method)
05-24 01:48:08.028: E/AndroidRuntime(23665): at java.lang.reflect.Method.invoke(Method.java:515)
05-24 01:48:08.028: E/AndroidRuntime(23665): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-24 01:48:08.028: E/AndroidRuntime(23665): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-24 01:48:08.028: E/AndroidRuntime(23665): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
05-24 01:48:08.028: E/AndroidRuntime(23665): at dalvik.system.NativeStart.main(Native Method)
05-24 01:48:08.028: E/AndroidRuntime(23665): Caused by: java.lang.NullPointerException
05-24 01:48:08.028: E/AndroidRuntime(23665): at android.app.PendingIntent.getActivity(PendingIntent.java:261)
05-24 01:48:08.028: E/AndroidRuntime(23665): at android.app.PendingIntent.getActivity(PendingIntent.java:224)
05-24 01:48:08.028: E/AndroidRuntime(23665): at com.koshmen.clock.aimen.Yarab.update(Yarab.java:251)
05-24 01:48:08.028: E/AndroidRuntime(23665): at com.koshmen.clock.aimen.Yarab.onReceive(Yarab.java:226)
05-24 01:48:08.028: E/AndroidRuntime(23665): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2419)
05-24 01:48:08.028: E/AndroidRuntime(23665): ... 14 more
05-24 01:48:09.783: I/Process(23665): Sending signal. PID: 23665 SIG: 9
答案 0 :(得分:1)
NullPointerException
来自方法PendingIntent.getActivity()
,并且在尝试调用您在此调用中传递的Intent
参数上的方法时发生:
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);
变量i
为null
。
在您的代码中,您执行此操作:
i = packageManager.getLaunchIntentForPackage(packageName);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);
由于PackageManager.getLaunchIntentForPackage()
的来电可能会返回null
,因此您需要添加检查该条件的代码。
答案 1 :(得分:0)
我在此行中将 true 替换为 false 解决了这个问题:
prefsFile.getBoolean(String.valueOf(nn) + "app_onclick", true)) {
现在它正在处理所有真实的设备和模拟器。