gpt4 book ai didi

Android - TabHost/TabWidget

转载 作者:行者123 更新时间:2023-11-29 21:31:30 28 4
gpt4 key购买 nike

我是 android 新手,尝试移植 iOS 应用程序。不幸的是,我的基本设置无法正常工作。

我正在尝试实现与本教程类似的导航: tutorial

它或多或少是一个包含多个选项卡的简单 TabHost但不是使用

tabHost.addTab(tabHost.newTabSpec("settings").setIndicator("settings").setContent(R.id.tab1));

在教程中以及正在运行的内容中,我想用这样的类初始化我的选项卡:

tabHost.addTab(tabHost.newTabSpec("settings").setIndicator("settings").setContent(new Intent(this, SettingsActivity.class)));

不幸的是,当我点击“设置标签”时,应用程序崩溃了。

到目前为止,这是我的代码:

MainActivity:

package xxx;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

public class MainActivity extends Activity implements OnTabChangeListener
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTabs();
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


private void initTabs()
{
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();


tabHost.addTab(tabHost.newTabSpec("Übersicht").setIndicator("Übersicht").setContent(R.id.tab1)); // <- is working fine
tabHost.addTab(tabHost.newTabSpec("Einstellungen").setIndicator("Einstellungen").setContent(new Intent(this, SettingsActivity.class))); <- crash

tabHost.setOnTabChangedListener(this);

tabHost.setCurrentTab(0);
}


@Override
public void onTabChanged(String tabId)
{
// TODO Auto-generated method stub
}
}

activity_main.xml:

<RelativeLayout xmlns:android=
xmlns:tools=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

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

<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" >
</TabWidget>

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

<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

</LinearLayout>

<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

</LinearLayout>

<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

</LinearLayout>

<LinearLayout
android:id="@+id/tab4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

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

设置 Activity :

package xxx;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SettingsActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

TextView tv = new TextView(this);
tv.setText("This is tab 2");
setContentView(tv);
}
}

来自 LogCat 的错误消息:

10-15 03:52:22.711: W/dalvikvm(889): threadid=1: thread exiting with uncaught exception (group=0x41465700) 10-15 03:52:22.851: E/AndroidRuntime(889): FATAL EXCEPTION: main 10-15 03:52:22.851: E/AndroidRuntime(889): java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'? 10-15 03:52:22.851: E/AndroidRuntime(889): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:747) 10-15 03:52:22.851: E/AndroidRuntime(889): at android.widget.TabHost.setCurrentTab(TabHost.java:413) 10-15 03:52:22.851: E/AndroidRuntime(889): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154) 10-15 03:52:22.851: E/AndroidRuntime(889): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546) 10-15 03:52:22.851: E/AndroidRuntime(889): at android.view.View.performClick(View.java:4240) 10-15 03:52:22.851: E/AndroidRuntime(889): at android.view.View$PerformClick.run(View.java:17721) 10-15 03:52:22.851: E/AndroidRuntime(889): at android.os.Handler.handleCallback(Handler.java:730) 10-15 03:52:22.851: E/AndroidRuntime(889): at android.os.Handler.dispatchMessage(Handler.java:92) 10-15 03:52:22.851: E/AndroidRuntime(889): at android.os.Looper.loop(Looper.java:137) 10-15 03:52:22.851: E/AndroidRuntime(889): at android.app.ActivityThread.main(ActivityThread.java:5103) 10-15 03:52:22.851: E/AndroidRuntime(889): at java.lang.reflect.Method.invokeNative(Native Method) 10-15 03:52:22.851: E/AndroidRuntime(889): at java.lang.reflect.Method.invoke(Method.java:525) 10-15 03:52:22.851: E/AndroidRuntime(889): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 10-15 03:52:22.851: E/AndroidRuntime(889): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-15 03:52:22.851: E/AndroidRuntime(889): at dalvik.system.NativeStart.main(Native Method)

我认为问题是,我的 MainActivity 不是从 ActivityGroup 扩展的,和/或我没有使用 LocalActivityManager。问题是,两者都已弃用。我必须更改什么才能在不使用已弃用的方法和类的情况下使其正常工作?

很抱歉这个可能很简单的问题,但我通过谷歌没有找到任何东西,而且我是 android 编程的新手:)。

最佳答案

正如您提到的,您应该从 ActivityGroup 扩展,但如果您不想要弃用的类,那么您可以使用 FragmentsFragmentManager :Fragments

关于Android - TabHost/TabWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19376473/

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