启动应用程序时出现 NullPointerException [英] NullPointerException on starting application
问题描述
我厌倦了开始我的 Activity(在 AndroidManifest 中声明),但后来我看到了那个错误:
I tired to start my Activity (declared in AndroidManifest) , but then I saw that error(s) :
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 行:
That means that error is on 107th line :
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);
}
我不知道为什么我有那个错误.该方法是这样调用的:
I don't know why I have that error. That method is called like that :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Some code has been removed */
if(dcm.isAdminActive(c))
ustawWidokMain();
推荐答案
我敢猜测您是在 onCreate()
之外初始化 pm
.这将尝试在您实际拥有 Context
之前对其进行初始化,它会返回 null
.您需要在 onCreate()
内部和调用使用它的函数之前执行此操作.
I would venture to guess that you are initializing pm
outside of onCreate()
. This would try initializing it before you actually had a Context
and it would return null
. You need to do this inside of onCreate()
and before calling the function that uses it.
PackageManager pm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pm = getPackageManager();
/* Some code has been removed */
if(dcm.isAdminActive(c))
ustawWidokMain();
任何需要 Activity
内的 Context
都需要在 onCreate()
中初始化,因为 >Activity
和它的 Context
在此之前没有初始化.
Anything that needs a Context
inside of an Activity
needs to be initialized, at the earliest, in onCreate()
since the Activity
and its Context
aren't initialized before this.
这篇关于启动应用程序时出现 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!