0

我的 ApkInfo 活动应显示权限以及权限说明。但在运行时,它会以未知格式显示权限描述。在以下代码中,AppData 类用于初始化 packageInfo,其中用户选择的应用从已安装的应用列表中填充。

public class ApkInfo extends Activity { 

PackageInfo packageInfo;
PermissionInfo pi;
PackageManager pm;    

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.apkinfo);
PermissionInfo pi = new PermissionInfo();
AppData appData = (AppData) getApplicationContext();
packageInfo = appData.getPackageInfo(); 
if (packageInfo.requestedPermissions != null){
String[] permissions = getPermissions(packageInfo.requestedPermissions, pi);
}
private String[] getPermissions(String[] requestedPermissions, PermissionInfo pinfo) {
pm = getPackageManager();
String permission = "";
for (int i = 0; i < requestedPermissions.length; i++) {
try {
pinfo = pm.getPermissionInfo(requestedPermissions[i], PackageManager.GET_META_DATA);

try {

pinfo.loadDescription(pm).toString();

}catch(NullPointerException ex){

              ex.printStackTrace();

}finally{

Log.d("Permissions description", "Null");
}
} catch (NameNotFoundException e) { 
  e.printStackTrace();  
}
permission = permission + requestedPermissions[i] + "    " + pinfo + "\n";    
Log.d("PERMISSIONS" , pm.toString());
}
String lines[] = permission.split("\\n");
return lines;
}
}

这是我的 logcat 输出:

06-25 11:35:00.741: D/Permissions description(776): Null
06-25 11:35:00.741: D/PERMISSION DESCRIPTION(776): PermissionInfo{40da5f10 android.permission.CAMERA}
06-25 11:35:00.754: W/System.err(776): java.lang.NullPointerException
06-25 11:35:00.781: W/System.err(776):  at com.ibc.android.demo.appslist.activity.ApkInfo.getPermissions(ApkInfo.java:54)
06-25 11:35:00.781: W/System.err(776):  at com.ibc.android.demo.appslist.activity.ApkInfo.onCreate(ApkInfo.java:34)
06-25 11:35:00.781: W/System.err(776):  at android.app.Activity.performCreate(Activity.java:5104)
06-25 11:35:00.793: W/System.err(776):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-25 11:35:00.793: W/System.err(776):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-25 11:35:00.812: W/System.err(776):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-25 11:35:00.812: W/System.err(776):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-25 11:35:00.812: W/System.err(776):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-25 11:35:00.821: W/System.err(776):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 11:35:00.821: W/System.err(776):  at android.os.Looper.loop(Looper.java:137)
06-25 11:35:00.831: W/System.err(776):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-25 11:35:00.841: W/System.err(776):  at java.lang.reflect.Method.invokeNative(Native Method)
06-25 11:35:00.841: W/System.err(776):  at java.lang.reflect.Method.invoke(Method.java:511)
06-25 11:35:00.851: W/System.err(776):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-25 11:35:00.851: W/System.err(776):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-25 11:35:00.851: W/System.err(776):  at dalvik.system.NativeStart.main(Native Method)
06-25 11:35:00.861: D/Permissions description(776): Null
06-25 11:35:00.861: D/PERMISSION DESCRIPTION(776): PermissionInfo{40da7378 android.permission.RECORD_AUDIO}
06-25 11:35:00.891: D/Permissions description(776): Null
06-25 11:35:00.891: D/PERMISSION DESCRIPTION(776): PermissionInfo{40daa998 android.permission.ACCESS_FINE_LOCATION}
06-25 11:35:00.931: D/Permissions description(776): Null
06-25 11:35:00.931: D/PERMISSION DESCRIPTION(776): PermissionInfo{40dab3b8 android.permission.WAKE_LOCK}
06-25 11:35:00.964: D/Permissions description(776): Null
06-25 11:35:00.964: D/PERMISSION DESCRIPTION(776): PermissionInfo{40dabf28 android.permission.SET_WALLPAPER}
06-25 11:35:00.991: D/Permissions description(776): Null
06-25 11:35:00.991: D/PERMISSION DESCRIPTION(776): PermissionInfo{40dacaa8 android.permission.WRITE_EXTERNAL_STORAGE}
06-25 11:35:01.021: D/Permissions description(776): Null
06-25 11:35:01.021: D/PERMISSION DESCRIPTION(776): PermissionInfo{40dad588 android.permission.READ_SMS}
06-25 11:35:01.041: D/Permissions description(776): Null
06-25 11:35:01.041: D/PERMISSION DESCRIPTION(776): PermissionInfo{40dae020 android.permission.READ_EXTERNAL_STORAGE}

为什么我的 ApkInfo 活动以这种方式显示权限描述?对于某些应用程序,它会显示 NullPointerException。

4

1 回答 1

0

PermissionInfo 的 loadDescription 方法返回一个 charSequence。在我的代码中,我试图使用 toString 方法将从语句 pinfo.loadDescription(pm) 返回的描述信息转换为字符串,但该方法无法给我确切的字符串。使用 stringBuilder 解决了这个问题。

CharSequence c = pinfo.loadDescription(pm);
StringsBuilder sb = new StringBuilder(c.length());
sb.append(c);
sb.toString();

这是部分答案。我还没有回答空指针异常。仍在试图找出背后的原因。

于 2013-07-16T11:15:39.900 回答