检索权限说明时出错

时间:2013-06-25 12:26:13

标签: android android-permissions

我的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。

1 个答案:

答案 0 :(得分: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();

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