gpt4 book ai didi

android - getPackageManger() 完成时出现 NullPointerexception

转载 作者:行者123 更新时间:2023-11-29 00:31:00 25 4
gpt4 key购买 nike

我是 android 编码的新手,我遇到了一个错误,如果有人可以帮助我修复错误,我将非常感激。尽管错误可能很愚蠢,但请尽可能提供解决方案。

package com.example.helloworld;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PermissionInfo;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class CheckPermissions extends Activity {
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.check);
}

ArrayList<String> list_permission = new ArrayList<String>();
String reqper[] = { "android.permission.ACCESS_CHECKIN_PROPERTIES",
"android.permission.ACCESS_COARSE_LOCATION" };
{

if (reqper != null) {
for (int i = 0; i < reqper.length; i++) {

String a = reqper[i];
if (a.contains("android.permission.")) {
try {

PermissionInfo pi = null;
PackageManager pm = context.getPackageManager();
pi = pm.getPermissionInfo(a,
PackageManager.GET_META_DATA);
String protctionLevel = "unknown";

switch (pi.protectionLevel) {
case PermissionInfo.PROTECTION_NORMAL:
protctionLevel = "normal";
break;
case PermissionInfo.PROTECTION_DANGEROUS:
protctionLevel = "dangerous";
break;
case PermissionInfo.PROTECTION_SIGNATURE:
protctionLevel = "signature";
break;
case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM:
protctionLevel = "signatureOrSystem";
break;
case PermissionInfo.PROTECTION_FLAG_SYSTEM:
protctionLevel = "system";
break;
default:
protctionLevel = "<unknown>";
break;
}
list_permission.add(a + " " + protctionLevel);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
list_permission.add(a);
}
}
}

}
TableLayout table = (TableLayout) findViewById(R.id.mytable);
{
for (int i = 0; i < list_permission.size(); i++) {
TableRow row = new TableRow(this);
String sol = list_permission.get(i);

TextView tvperm = new TextView(this);
tvperm.setText("" + sol);

row.addView(tvperm);

table.addView(row);
}
}
}

所以这是 Logcat-

03-29 18:34:57.201: W/Trace(1785): Unexpected value from nativeGetEnabledTags: 0
03-29 18:34:57.220: W/Trace(1785): Unexpected value from nativeGetEnabledTags: 0
03-29 18:34:57.270: D/AndroidRuntime(1785): Shutting down VM
03-29 18:34:57.270: W/dalvikvm(1785): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
03-29 18:34:57.411: E/AndroidRuntime(1785): FATAL EXCEPTION: main
03-29 18:34:57.411: E/AndroidRuntime(1785): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.helloworld/com.example.helloworld.CheckPermissions}: java.lang.NullPointerException
03-29 18:34:57.411: E/AndroidRuntime(1785): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-29 18:34:57.411: E/AndroidRuntime(1785): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-29 18:34:57.411: E/AndroidRuntime(1785): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-29 18:34:57.411: E/AndroidRuntime(1785): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-29 18:34:57.411: E/AndroidRuntime(1785): at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 18:34:57.411: E/AndroidRuntime(1785): at android.os.Looper.loop(Looper.java:137)
03-29 18:34:57.411: E/AndroidRuntime(1785): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-29 18:34:57.411: E/AndroidRuntime(1785): at java.lang.reflect.Method.invokeNative(Native Method)
03-29 18:34:57.411: E/AndroidRuntime(1785): at java.lang.reflect.Method.invoke(Method.java:511)
03-29 18:34:57.411: E/AndroidRuntime(1785): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-29 18:34:57.411: E/AndroidRuntime(1785): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-29 18:34:57.411: E/AndroidRuntime(1785): at dalvik.system.NativeStart.main(Native Method)
03-29 18:34:57.411: E/AndroidRuntime(1785): Caused by: java.lang.NullPointerException
03-29 18:34:57.411: E/AndroidRuntime(1785): at android.app.Activity.findViewById(Activity.java:1839)
03-29 18:34:57.411: E/AndroidRuntime(1785): at com.example.helloworld.CheckPermissions.<init>(CheckPermissions.java:29)
03-29 18:34:57.411: E/AndroidRuntime(1785): at java.lang.Class.newInstanceImpl(Native Method)
03-29 18:34:57.411: E/AndroidRuntime(1785): at java.lang.Class.newInstance(Class.java:1319)
03-29 18:34:57.411: E/AndroidRuntime(1785): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-29 18:34:57.411: E/AndroidRuntime(1785): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-29 18:34:57.411: E/AndroidRuntime(1785): ... 11 more

最佳答案

您应该创建一个初始化方法,并在为上下文赋值后显式调用它,而不是使用初始化代码。像这样:

TableLayout table;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.check);

context=CheckPermissions.this;
table = (TableLayout) findViewById(R.id.mytable);
initializeValues();
}

private void initializeValues(){
if (reqper != null) {
for (int i = 0; i < reqper.length; i++) {

String a = reqper[i];
if (a.contains("android.permission.")) {
try {

PermissionInfo pi = null;
PackageManager pm = context.getPackageManager();
pi = pm.getPermissionInfo(a,
PackageManager.GET_META_DATA);
String protctionLevel = "unknown";

switch (pi.protectionLevel) {
case PermissionInfo.PROTECTION_NORMAL:
protctionLevel = "normal";
break;
case PermissionInfo.PROTECTION_DANGEROUS:
protctionLevel = "dangerous";
break;
case PermissionInfo.PROTECTION_SIGNATURE:
protctionLevel = "signature";
break;
case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM:
protctionLevel = "signatureOrSystem";
break;
case PermissionInfo.PROTECTION_FLAG_SYSTEM:
protctionLevel = "system";
break;
default:
protctionLevel = "<unknown>";
break;
}
list_permission.add(a + " " + protctionLevel);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
list_permission.add(a);
}
}
}
for (int i = 0; i < list_permission.size(); i++) {
TableRow row = new TableRow(this);
String sol = list_permission.get(i);

TextView tvperm = new TextView(this);
tvperm.setText("" + sol);

row.addView(tvperm);

table.addView(row);
}
}

我冒昧地将两个初始化部分合并到新方法中。

关于android - getPackageManger() 完成时出现 NullPointerexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15708624/

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