Reputation: 31
I'm totally new to android coding and I'm stuck with an error, I'll be highly obliged if someone could help me fixing the bug. Though the mistake might be stupid, please do provide a solution if possible.
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);
}
}
}
so here is the 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
Upvotes: 2
Views: 1895
Reputation: 5568
Instead of using an initialization code, you should create an initialization method and call it explicitly after you have assigned context a value. Like this:
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);
}
}
I took the liberty of incorporating both initialization sections to the new method.
Upvotes: 1
Reputation: 132972
you forget to initialize context
before using it.initialize it inside onCreate
method as :
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.check);
context=CheckPermissions.this; //<<< initialize context here
}
Upvotes: 1