2013-07-15 77 views
1

我跟着this教程來實現我的android應用程序的選項卡布局。我已經有一個現有的android應用程序,我正在開發,所以我不得不調整一下教程。我敢肯定,我所做的一切都完全按照我應有的方式進行,但每次點擊應該打開標籤佈局活動的按鈕時,我的應用都會崩潰。我的應用程序崩潰時,打開tablayout活動

主要tabLayout活動:

public class Bifrosttabs extends TabActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_bifrosttabs); 

    TabHost tabHost = getTabHost(); 

    //CRAFTING TAB 
    TabSpec craftTabSpec = tabHost.newTabSpec("Crafting"); 
    craftTabSpec.setIndicator("Crafting", getResources().getDrawable(R.drawable.weaponsmith_logo)); 
    Intent craftIntent = new Intent(this, Bifrost.class); 
    craftTabSpec.setContent(craftIntent); 

    //ADDITION INFO TAB 
    TabSpec infoTabSpec = tabHost.newTabSpec("Info"); 
    infoTabSpec.setIndicator("Info", getResources().getDrawable(R.drawable.craft_info)); 
    Intent infoIntent = new Intent(this, Bifrostinfo.class); 
    infoTabSpec.setContent(infoIntent); 

    tabHost.addTab(craftTabSpec); 
    tabHost.addTab(infoTabSpec); 
} 

主要tabLayout活動的XML文件:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 
</LinearLayout> 

打開的選項卡布局活性

按鈕方法:

public void onClick (View v){ 
    switch (v.getId()){ 
    case R.id.button1: 
     Intent bifrost = new Intent (MainActivity.this, Bifrosttabs.class); 
     startActivity(bifrost); 
     break; 
...rest of the code for other buttons 

瓦ole Logcat文件:

07-15 17:57:00.084: E/jdwp(14846): Failed writing handshake bytes: Broken pipe (-1 of 14) 
07-15 17:57:06.186: E/AndroidRuntime(14846): FATAL EXCEPTION: main 
07-15 17:57:06.186: E/AndroidRuntime(14846): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.matthewstudios.gw2legendary/com.example.gw2legendary.tablayouts.Bifrosttabs}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.matthewstudios.gw2legendary/com.example.gw2legendary.craftingactivities.Bifrost}: java.lang.NullPointerException 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.os.Looper.loop(Looper.java:137) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.app.ActivityThread.main(ActivityThread.java:5041) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at java.lang.reflect.Method.invokeNative(Native Method) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at java.lang.reflect.Method.invoke(Method.java:511) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at dalvik.system.NativeStart.main(Native Method) 
07-15 17:57:06.186: E/AndroidRuntime(14846): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.matthewstudios.gw2legendary/com.example.gw2legendary.craftingactivities.Bifrost}: java.lang.NullPointerException 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2023) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.widget.TabHost.setCurrentTab(TabHost.java:413) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.widget.TabHost.addTab(TabHost.java:240) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at com.example.gw2legendary.tablayouts.Bifrosttabs.onCreate(Bifrosttabs.java:34) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.app.Activity.performCreate(Activity.java:5104) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
07-15 17:57:06.186: E/AndroidRuntime(14846): ... 11 more 
07-15 17:57:06.186: E/AndroidRuntime(14846): Caused by: java.lang.NullPointerException 
07-15 17:57:06.186: E/AndroidRuntime(14846): at com.example.gw2legendary.craftingactivities.Bifrost.onCreate(Bifrost.java:160) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.app.Activity.performCreate(Activity.java:5104) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
07-15 17:57:06.186: E/AndroidRuntime(14846): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
07-15 17:57:06.186: E/AndroidRuntime(14846): ... 21 more 
07-15 17:57:14.967: E/fb4a(:<default>):MmsConfig(14952): MmsConfig.loadMmsSettings mms_config.xml missing uaProfUrl setting 

我很確定我給了你所有的重要代碼。如果我忘記了任何東西,請告訴我。

+1

你的logcat中的NullPointerException之後會發生什麼?這很可能是您的問題所在 – CodingIntrigue

+0

@ Blade0rz我只是粘貼了整個logcat文件,請檢查我的編輯 – Guy

+6

Bifrost類的行號160上的錯誤。你還沒有啓動一些變量。 – Laksh

回答

0

感謝this user,我想通了!在我的班級bifrost中,第160行出現了問題。

ActionBar actionBar = getActionBar(); 
actionBar.setDisplayHomeAsUpEnabled(true); 

這兩行引起了所有的麻煩!我猜你不能啓用操作欄,如果你有標籤佈局。

相關問題