[英]Android NullPointerException in OnCreate method
I have been writing this Android application for the past few days and everything has been working smoothly, until today. 在过去的几天里,我一直在编写此Android应用程序,直到今天,一切都一直在顺利进行。 In my app I was trying to implement a tabbed view for my application, but as soon as I try to run it my application installs and force closes immediately upon opening.
在我的应用程序中,我试图为我的应用程序实现一个选项卡式视图,但是一旦我尝试运行它,我的应用程序就会安装并在打开后立即关闭。 I have been sitting at this for an entire day and cannot for the life of me figure out what the issue is.
我已经整整一天坐在这里,无法终生了解问题所在。 Hopefully someone can help me out.
希望有人可以帮助我。
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
Here is the code for the main activity: 这是主要活动的代码:
public class LoLBuffTimerMain extends TabActivity{
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intent = new Intent().setClass(this, YourBuffs.class);
spec = tabHost.newTabSpec("yourbuffs").setIndicator("Your Buffs",
res.getDrawable(R.drawable.tab_view_you))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, EnemyBuffs.class);
spec = tabHost.newTabSpec("enemybuffs").setIndicator("Enemy Buffs",
res.getDrawable(R.drawable.tab_view_enemy))
.setContent(intent);
tabHost.addTab(spec);
}
}
and the XML: 和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>
The stack trace points to this line as the source of the exception: 堆栈跟踪指向此行作为异常源:
Resources res = getResources();
The inherited getResources()
method is throwing an exception. 继承的
getResources()
方法引发异常。 That's because you can't call getResources()
until later, after the activity is constructed and initialized. 这是因为在构造和初始化活动之后,您才能稍后调用
getResources()
。 Move those member variable initializations into onCreate()
and you should be good to go. 将那些成员变量初始化移到
onCreate()
,您应该一切顺利。
The following lines need to be defined inside onCreate 需要在onCreate中定义以下行
Resources res = getResources();
TabHost tabHost = getTabHost();
I'm not sure of this, but I think you can do a getTabHost()
only after you do a setContentView()
. 我不确定,但是我认为只有在执行
setContentView()
之后才能执行getTabHost()
setContentView()
。 Otherwise getTabHost() returns null (this behavior is similar to findViewById()
). 否则,getTabHost()返回null(此行为类似于
findViewById()
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.