Java OnCreate方法中的Android NullPointerException
在过去的几天里,我一直在编写这个Android应用程序,直到今天,一切都很顺利。在我的应用程序中,我试图为我的应用程序实现一个选项卡式视图,但一旦我尝试运行它,我的应用程序就会安装,并在打开后立即强制关闭。我已经坐在这里一整天了,我一辈子都搞不清楚问题是什么。希望有人能帮我Java OnCreate方法中的Android NullPointerException,java,android,android-layout,nullpointerexception,android-manifest,Java,Android,Android Layout,Nullpointerexception,Android Manifest,在过去的几天里,我一直在编写这个Android应用程序,直到今天,一切都很顺利。在我的应用程序中,我试图为我的应用程序实现一个选项卡式视图,但一旦我尝试运行它,我的应用程序就会安装,并在打开后立即强制关闭。我已经坐在这里一整天了,我一辈子都搞不清楚问题是什么。希望有人能帮我 E/AndroidRuntime(5469): FATAL EXCEPTION: main E/AndroidRuntime(5469): java.lang.RuntimeException: Unable to inst
E/AndroidRuntime(5469): FATAL EXCEPTION: main
E/AndroidRuntime(5469): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo
{com.stullich.tim.lolbufftimer/com.stullich.tim.lolbufftimer.LoLBuffTimerMain}: java.lang.NullPointerException
E/AndroidRuntime(5469):at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:1573)
E/AndroidRuntime(5469): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
E/AndroidRuntime(5469): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime(5469): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
E/AndroidRuntime(5469): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(5469): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(5469): at android.app.ActivityThread.main(ActivityThread.java:3691)
E/AndroidRuntime(5469): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(5469): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(5469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:907)
E/AndroidRuntime(5469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
E/AndroidRuntime(5469): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(5469): Caused by: java.lang.NullPointerException
E/AndroidRuntime(5469): at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
E/AndroidRuntime(5469): at com.stullich.tim.lolbufftimer.LoLBuffTimerMain.<init>(LoLBuffTimerMain.java:14)
E/AndroidRuntime(5469): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(5469): at java.lang.Class.newInstance(Class.java:1409)
E/AndroidRuntime(5469): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime(5469): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
E/AndroidRuntime(5469): ... 11 more
以及XML:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
</FrameLayout>
</TabHost>
堆栈跟踪指向此行作为异常源:
Resources res = getResources();
继承的
getResources()
方法正在引发异常。这是因为在构建和初始化活动之前,您不能调用getResources()
。将这些成员变量初始化移到onCreate()
中,您就可以开始了。堆栈跟踪指向这一行作为异常源:
Resources res = getResources();
继承的
getResources()
方法正在引发异常。这是因为在构建和初始化活动之前,您不能调用getResources()
。将这些成员变量初始化移到onCreate()
中,您就可以开始了。以下几行需要在onCreate中定义
Resources res = getResources();
TabHost tabHost = getTabHost();
需要在onCreate中定义以下行
Resources res = getResources();
TabHost tabHost = getTabHost();
我不确定这一点,但我认为只有在执行
setContentView()
之后才能执行getTabHost()
。否则,getTabHost()将返回null(此行为类似于findViewById()
)。我不确定这一点,但我认为只有在执行setContentView()
之后才能执行getTabHost()
。否则,getTabHost()将返回null(此行为类似于findViewById()
)