gpt4 book ai didi

android - Android Tabhost 有问题

转载 作者:行者123 更新时间:2023-11-30 00:41:17 35 4
gpt4 key购买 nike

我想在我的自定义对话框中使用标签。

这是我得到的:

[project_settings.xml]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#ffffffff"
android:layout_height="450dip">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:id="@+id/text_dialog"
android:layout_below="@+id/a"
android:layout_marginTop="20dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="20dp"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />

<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="OK"
android:id="@+id/btn_dialog"
android:gravity="center_vertical|center_horizontal"
android:layout_below="@+id/text_dialog"
android:layout_marginBottom="20dp"
android:background="@android:color/holo_green_dark"
android:layout_centerHorizontal="true"
android:textColor="#ffffffff" />

<ImageView
android:layout_width="match_parent"
android:id="@+id/a"
android:gravity="center"
android:background="#DA5F6A"
android:scaleType="fitCenter"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:layout_height="100dp" />

<TabHost
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_height="match_parent"
android:id="@+id/tabhost">

<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:layout_marginTop="44dp">

</TabWidget>

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

<LinearLayout
android:id="@+id/general"
android:text="OK"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

</LinearLayout>

<LinearLayout
android:id="@+id/resources"
android:text="OK"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

</LinearLayout>

<LinearLayout
android:id="@+id/variables"
android:text="OK"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

</LinearLayout>

</FrameLayout>
</LinearLayout>
</TabHost>

[ProjectSettings.java]

public class ProjectSettingsWindow {
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.project_settings);

// create the TabHost that will contain the Tabs
TabHost tabHost = (TabHost)dialog.findViewById(R.id.tabhost);


TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab");

tab1.setIndicator("Tab1");
tab1.setContent(new Intent(""));

tab2.setIndicator("Tab2");
tab2.setContent(new Intent(""));

tab3.setIndicator("Tab3");
tab3.setContent(new Intent(""));

tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);


TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);

Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

dialog.show();

}
}

如何制作原生的工作标签?为什么它不只显示 XML?我收到 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TabWidget.addView(android.view.View)' on a null object reference 执行 时出错tabhost.addTab() 即使有这样的布局和这样的 ID。

这是它在 XML 中的样子: enter image description here

最佳答案

您需要在添加标签之前设置() tabHost!

尝试添加以下代码行:

    LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false); 
mLocalActivityManager.dispatchCreate(savedInstanceState);
// create the TabHost that will contain the Tabs
TabHost tabHost = (TabHost)dialog.findViewById(R.id.tabhost);
tabHost.setup(mLocalActivityManager);

TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");

关于android - Android Tabhost 有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42540364/

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