我对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