2

我有一个带有两个选项卡的选项卡主机:一个用于图片库,另一个用于打开页面。TabHost 独立工作正常,但是当我想通过以前的活动访问它时,应用程序崩溃了。谁能告诉我问题是什么?我是否必须添加某些内容或我正在做的事情都是错误的。请告诉我是否有其他方法。我的代码如下:

public class DelhimapActivity extends TabActivity{

   TabHost tab;
   Intent intent;
   GridView grid;

   public void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.delhimap);
      tab = getTabHost();

      tab.addTab(tab.newTabSpec("tab1").setIndicator("Images").setContent(R.id.gridView1));
      tab.addTab(tab.newTabSpec("tab2").setIndicator("About").setContent(intent));
      grid = (GridView) findViewById(R.id.gridView1);
      grid.setAdapter(new ImageAdapter(this));
      intent.setClass(this, DelhiLay.class);
      startActivity(intent);

      tab.setCurrentTab(0);
   }

}

我的图像适配器:

public class ImageAdapter extends BaseAdapter {
   private Context mContext;

   public ImageAdapter(Context c) {
      // TODO Auto-generated constructor stub
      mContext= c;
   }

   public int getCount() {
      // TODO Auto-generated method stub
      return mThumbIds.length;
   }

   public Object getItem(int position) {
      // TODO Auto-generated method stub
      return null;
   }

   public long getItemId(int position) {
      // TODO Auto-generated method stub
      return 0;
   }

   public View getView(int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      ImageView imageView;
      if (convertView == null) {  // if it's not recycled, initialize some attributes
         imageView = new ImageView(mContext);            

         imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
         imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
         imageView.setPadding(8, 8, 8, 8);

         //myButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL));

      } else {
         imageView = (ImageView) convertView;
      }

      imageView.setImageResource(mThumbIds[position]);
      return imageView;
   }

  // references to our images
  private Integer[] mThumbIds = {
        R.drawable.ben, R.drawable.brent,
        R.drawable.cean,
        R.drawable.charters,
        R.drawable.lynn,
        R.drawable.mark,
        R.drawable.paul,
        R.drawable.philip
  };
} 

并从这样的另一个活动中访问它:

public void onEnter(View v){

      prg = ProgressDialog.show(this, "", "Loading...");
      new Handler().postDelayed(new Runnable() {

            public void run() {
      Intent inta = new Intent(NewActivity.this,DelhimapActivity.class);
      NewActivity.this.startActivity(inta);
      NewActivity.this.finish();
            }
      },3000
      );
}

这是具有 TabHost 的 XML:

<?xml version="1.0" encoding="utf-8"?>


<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:tabStripEnabled="true">
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <GridView
                android:id="@+id/gridView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:numColumns="3" >
            </GridView>
            <LinearLayout 
                android:id="@+id/linear"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </LinearLayout>

        </FrameLayout>

    </LinearLayout>

这是我用于此活动的 LogCat:

08-23 13:56:46.008: E/AndroidRuntime(365): FATAL EXCEPTION: main
**08-23 13:56:46.008: E/AndroidRuntime(365): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.delhicious.suryastra/com.delhicious.suryastra.DelhimapActivity}: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'**
4

2 回答 2

1

Download Code example from below link, it may help you.

Tab Sample

于 2012-08-23T05:14:02.847 回答
0

在您的布局文件中替换@+id/tabhost为。@android:id/tabhost

于 2012-08-23T08:56:10.567 回答