gpt4 book ai didi

android - 运行时异常 : Unable to start receiver "package name" : NullPointerException

转载 作者:行者123 更新时间:2023-11-30 02:52:50 27 4
gpt4 key购买 nike

我知道这个问题是重复的。但是我在这里找不到合适的解决方案。我正在开发一个 Android 时钟小部件,并且在我的 Nexus 4 和我兄弟的 hTC One X 设备上运行顺利。但在其他设备上,如 Galaxy SII、SIII 和 Nexus 10,应用程序会在配置 Activity 出现后崩溃,并显示由于 NPE 导致的 Force Stop 消息:

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);
}

接收时:

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);
}
}
}

list :

 <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

最佳答案

NullPointerException 来自 PendingIntent.getActivity() 方法,它发生在尝试调用 Intent 参数的方法时你通过这个电话:

     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);

变量inull

在您的代码中,您这样做:

     i = packageManager.getLaunchIntentForPackage(packageName);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);

因为调用 PackageManager.getLaunchIntentForPackage() 可能会返回 null,所以您需要添加代码来检查该条件。

关于android - 运行时异常 : Unable to start receiver "package name" : NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23837711/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com