gpt4 book ai didi

android - 从 Android 中的另一个 Activity 访问 TabHost

转载 作者:行者123 更新时间:2023-11-29 02:02:10 24 4
gpt4 key购买 nike

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

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
};
}

然后像这样从另一个 Activity 访问它:

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>

这是我的此 Activity 的 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'**

最佳答案

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

Tab Sample

关于android - 从 Android 中的另一个 Activity 访问 TabHost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12073040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com