从Android中的另一个Activity访问TabHost

时间:2012-08-22 12:21:38

标签: android android-widget

我有一个带有两个标签的标签主机:一个用于图库,另一个用于打开页面。 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);
   }

}

我的ImageAdapter:

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'**

2 个答案:

答案 0 :(得分:1)

从下面的链接下载代码示例,它可能对您有帮助。

Tab Sample

答案 1 :(得分:0)

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