Android垂直标签布局

时间:2012-05-25 12:39:19

标签: android android-tabhost android-tablelayout

我在运行时遇到此错误但没有编译错误?这是main的logcat和代码。

05-25 12:29:25.543: W/dalvikvm(384): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-25 12:29:25.582: E/AndroidRuntime(384): FATAL EXCEPTION: main
05-25 12:29:25.582: E/AndroidRuntime(384): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tablayout/com.example.tablayout.TablayoutActivity}: java.lang.NullPointerException
05-25 12:29:25.582: E/AndroidRuntime(384):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-25 12:29:25.582: E/AndroidRuntime(384):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-25 12:29:25.582: E/AndroidRuntime(384):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-25 12:29:25.582: E/AndroidRuntime(384):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-25 12:29:25.582: E/AndroidRuntime(384):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 12:29:25.582: E/AndroidRuntime(384):  at android.os.Looper.loop(Looper.java:123)
05-25 12:29:25.582: E/AndroidRuntime(384):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-25 12:29:25.582: E/AndroidRuntime(384):  at java.lang.reflect.Method.invokeNative(Native Method)
05-25 12:29:25.582: E/AndroidRuntime(384):  at java.lang.reflect.Method.invoke(Method.java:521)
05-25 12:29:25.582: E/AndroidRuntime(384):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-25 12:29:25.582: E/AndroidRuntime(384):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-25 12:29:25.582: E/AndroidRuntime(384):  at dalvik.system.NativeStart.main(Native Method)
05-25 12:29:25.582: E/AndroidRuntime(384): Caused by: java.lang.NullPointerException
05-25 12:29:25.582: E/AndroidRuntime(384):  at android.app.Activity.setContentView(Activity.java:1647)
05-25 12:29:25.582: E/AndroidRuntime(384):  at android.app.TabActivity.ensureTabHost(TabActivity.java:114)
05-25 12:29:25.582: E/AndroidRuntime(384):  at android.app.TabActivity.getTabHost(TabActivity.java:136)
05-25 12:29:25.582: E/AndroidRuntime(384):  at com.example.tablayout.TablayoutActivity.<init>(TablayoutActivity.java:15)
05-25 12:29:25.582: E/AndroidRuntime(384):  at java.lang.Class.newInstanceImpl(Native Method)
05-25 12:29:25.582: E/AndroidRuntime(384):  at java.lang.Class.newInstance(Class.java:1429)
05-25 12:29:25.582: E/AndroidRuntime(384):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-25 12:29:25.582: E/AndroidRuntime(384):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-25 12:29:25.582: E/AndroidRuntime(384):  ... 11 more






    <?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"
      android:layout_height="fill_parent">
     <LinearLayout android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"
      android:baselineAligned="false">
    <FrameLayout android:layout_width="0dip" 
        android:layout_height="fill_parent" 
        android:layout_weight="0.2">
    <TabWidget android:id="@android:id/tabs" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:visibility="invisible"/>
        <LinearLayout android:layout_width="fill_parent"  
            android:layout_height="fill_parent"
            android:orientation="vertical">
            <Button android:layout_height="0dip" 
                android:layout_width="fill_parent" 
                android:layout_weight="1.0"
            android:background="@drawable/icon_photos_tab"               
                android:id="@+id/artist_id" 
                android:onClick="tabHandler"/>
            <Button android:layout_height="0dip" 
                android:layout_width="fill_parent" 
                android:layout_weight="1.0"
            android:background="@drawable/icon_videos_tab"  
                android:id="@+id/album_id" 
                android:onClick="tabHandler"/>
            <Button android:layout_height="0dip" 
                android:layout_width="fill_parent" 
                android:layout_weight="1.0"
            android:background="@drawable/icon_songs_tab"   
                android:id="@+id/song_id" 
                android:onClick="tabHandler"/>
    </LinearLayout> 
    </FrameLayout>       
    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="0.8"/>
    </LinearLayout>
    </TabHost>

这里是main.java文件,这样你就可以看到它是一个全局变量以及我从哪里删除它。还将全局按钮添加到本地而不是本地按钮。

    import android.app.TabActivity;
    import android.content.Intent;
   import android.os.Bundle;
   import android.view.View;
   import android.widget.Button;
   import android.widget.TabHost;
   import android.widget.TabHost.TabSpec;

   public class TablayoutActivity extends TabActivity {
    /** Called when the activity is first created. */

Button artistButton, songButton, videosButton;
TabHost tabHost = getTabHost();

@Override
     public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //TabHost tabHost = getTabHost();

    // Tab for Photos
    TabSpec photospec = tabHost.newTabSpec("Photos");
    // setting Title and Icon for the Tab
    photospec.setIndicator("Photos",   getResources().getDrawable(R.drawable.icon_photos_tab));
    Intent photosIntent = new Intent(this, PhotosActivity.class);
    photospec.setContent(photosIntent);

    // Tab for Songs
    TabSpec songspec = tabHost.newTabSpec("Songs");
    songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
    Intent songsIntent = new Intent(this, SongsActivity.class);
    songspec.setContent(songsIntent);

    // Tab for Videos
    TabSpec videospec = tabHost.newTabSpec("Videos");
    videospec.setIndicator("Videos",         getResources().getDrawable(R.drawable.icon_videos_tab));
    Intent videosIntent = new Intent(this, VideosActivity.class);
    videospec.setContent(videosIntent);

    // Adding all TabSpec to TabHost
    tabHost.addTab(photospec); // Adding photos tab
    tabHost.addTab(songspec); // Adding songs tab
    tabHost.addTab(videospec); // Adding videos tab




   }

    public void tabHandler(View target){
    artistButton.setSelected(false);
    videosButton.setSelected(false);
    songButton.setSelected(false);
    if(target.getId() == R.id.artist_id){
        tabHost.setCurrentTab(0);
        artistButton.setSelected(true);
    } else if(target.getId() == R.id.album_id){
        tabHost.setCurrentTab(1);
        videosButton.setSelected(true);
    } else if(target.getId() == R.id.song_id){
        tabHost.setCurrentTab(2);
        songButton.setSelected(true);
    }
   };

    }

1 个答案:

答案 0 :(得分:1)

您可能正在尝试在构造函数中的某个位置调用getTabHost()或在类中设置字段。您必须在onCreate()内调用它,因为getTabHost()向下深度尝试获取当前窗口,该窗口尚未在构造函数时创建。

调用onCreate()之后的任何时间都没问题。