[英]Android NullPointerException in OnCreate method
在過去的幾天里,我一直在編寫此Android應用程序,直到今天,一切都一直在順利進行。 在我的應用程序中,我試圖為我的應用程序實現一個選項卡式視圖,但是一旦我嘗試運行它,我的應用程序就會安裝並在打開后立即關閉。 我已經整整一天坐在這里,無法終生了解問題所在。 希望有人可以幫助我。
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
這是主要活動的代碼:
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);
}
}
和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()
,您應該一切順利。
需要在onCreate中定義以下行
Resources res = getResources();
TabHost tabHost = getTabHost();
我不確定,但是我認為只有在執行setContentView()
之后才能執行getTabHost()
setContentView()
。 否則,getTabHost()返回null(此行為類似於findViewById()
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.