我厌倦了开始我的Activity(在AndroidManifest中声明),但后来我看到了这个错误:
08-11 16:04:05.952: E/AndroidRuntime(815): FATAL EXCEPTION: main
08-11 16:04:05.952: E/AndroidRuntime(815): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.radzik.devadmin/com.radzik.devadmin.MainActivity}: java.lang.NullPointerException
08-11 16:04:05.952: E/AndroidRuntime(815): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-11 16:04:05.952: E/AndroidRuntime(815): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-11 16:04:05.952: E/AndroidRuntime(815): at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-11 16:04:05.952: E/AndroidRuntime(815): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-11 16:04:05.952: E/AndroidRuntime(815): at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 16:04:05.952: E/AndroidRuntime(815): at android.os.Looper.loop(Looper.java:137)
08-11 16:04:05.952: E/AndroidRuntime(815): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-11 16:04:05.952: E/AndroidRuntime(815): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 16:04:05.952: E/AndroidRuntime(815): at java.lang.reflect.Method.invoke(Method.java:511)
08-11 16:04:05.952: E/AndroidRuntime(815): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-11 16:04:05.952: E/AndroidRuntime(815): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-11 16:04:05.952: E/AndroidRuntime(815): at dalvik.system.NativeStart.main(Native Method)
08-11 16:04:05.952: E/AndroidRuntime(815): Caused by: java.lang.NullPointerException
08-11 16:04:05.952: E/AndroidRuntime(815): at com.radzik.devadmin.MainActivity.ustawWidokMain(MainActivity.java:107)
08-11 16:04:05.952: E/AndroidRuntime(815): at com.radzik.devadmin.MainActivity.onCreate(MainActivity.java:35)
08-11 16:04:05.952: E/AndroidRuntime(815): at android.app.Activity.performCreate(Activity.java:5008)
08-11 16:04:05.952: E/AndroidRuntime(815): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-11 16:04:05.952: E/AndroidRuntime(815): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-11 16:04:05.952: E/AndroidRuntime(815): ... 11 more
这意味着错误在第107行:
private void ustawWidokMain()
{
setContentView(R.layout.activity_main);
Button b01 = (Button)findViewById(R.id.main_pass);
Button b02 = (Button)findViewById(R.id.main_wipes);
Button b03 = (Button)findViewById(R.id.main_camera);
Button b04 = (Button)findViewById(R.id.main_enctpyion);
Button b05 = (Button)findViewById(R.id.main_b1);
if(!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) && !pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) // there's error
{
b03.setEnabled(false);
}
我不知道为什么会有这个错误。该方法被称为:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Some code has been removed */
if(dcm.isAdminActive(c))
ustawWidokMain();
答案 0 :(得分:2)
我冒昧地猜测你是在pm
之外初始化onCreate()
。这会尝试在您实际拥有Context
之前初始化它,它会返回null
。您需要在onCreate()
内部并在调用使用它的函数之前执行此操作。
PackageManager pm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pm = getPackageManager();
/* Some code has been removed */
if(dcm.isAdminActive(c))
ustawWidokMain();
Context
内需要Activity
内的onCreate()
内的任何内容最早都需要在Activity
Context
进行初始化,因为{{1}}及其{{1}}在此之前初始化。
答案 1 :(得分:1)
您是否在Manifest中声明了权限?
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
什么是pm
?你在哪里实施它?好像你错过了。
答案 2 :(得分:0)
这意味着您没有在此ustawWidokMain方法中声明或初始化该对象。请重新检查或调试哪一行会给您带来错误。