我有一个受保护的布尔值,如下所示
protected boolean isKeyInstalled(Context context) {
// the packagename of the 'key' app
String proPackage = "com.funhouse.mytimmieskey";
// get the package manager
final PackageManager pm = context.getPackageManager();
// get a list of installed packages
List<PackageInfo> list = pm.getInstalledPackages(PackageManager.GET_DISABLED_COMPONENTS);
// let's iterate through the list
Iterator<PackageInfo> i = list.iterator();
while(i.hasNext()) {
PackageInfo p = i.next();
// check if proPackage is in the list AND whether that package is signed
// with the same signature as THIS package
if((p.packageName.equals(proPackage)) &&
(pm.checkSignatures(context.getPackageName(), p.packageName) == PackageManager.SIGNATURE_MATCH))
return true;
}
return false;
}
我试图让它显示广告,如果错误
if(isKeyInstalled(null) != false){
//AdMob Banner
LinearLayout parent = (LinearLayout) findViewById(R.id.adveiw);
AdView ad = new AdView(this, AdSize.BANNER, "a14db2c31f42ef0");
parent.addView(ad);
AdRequest r = new AdRequest();
r.setTesting(false);
ad.loadAd(r);
}
我没有代码错误,但这会不断兑现我的应用。
这里是logcat
05-11 00:18:37.253: INFO/ActivityManager(52): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.funhouse.mytimmies/.TimsMain bnds=[125,410][235,528] }
05-11 00:18:37.433: INFO/ActivityManager(52): Start proc com.funhouse.mytimmies for activity com.funhouse.mytimmies/.TimsMain: pid=1225 uid=10030 gids={3003}
05-11 00:18:37.814: DEBUG/ddm-heap(1225): Got feature list request
05-11 00:18:38.944: DEBUG/AndroidRuntime(1225): Shutting down VM
05-11 00:18:38.964: WARN/dalvikvm(1225): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-11 00:18:38.974: ERROR/AndroidRuntime(1225): Uncaught handler: thread main exiting due to uncaught exception
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.funhouse.mytimmies/com.funhouse.mytimmies.TimsMain}: java.lang.NullPointerException
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at android.os.Looper.loop(Looper.java:123)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at java.lang.reflect.Method.invoke(Method.java:521)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at dalvik.system.NativeStart.main(Native Method)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): Caused by: java.lang.NullPointerException
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at com.funhouse.mytimmies.TimsMain.isKeyInstalled(TimsMain.java:76)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at com.funhouse.mytimmies.TimsMain.onCreate(TimsMain.java:27)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-11 00:18:39.044: ERROR/AndroidRuntime(1225): ... 11 more
05-11 00:18:39.095: INFO/Process(52): Sending signal. PID: 1225 SIG: 3
答案 0 :(得分:1)
我猜final PackageManager pm = context.getPackageManager();
是你的错误发生的第76行,因为你传递的上下文是空的......因为在onCreate方法中发生错误所以它无法启动Activity ...传递this
(作为您的活动)作为上下文,它将解决您的问题。