我是安卓的新手,尝试移植一个iOS应用程序。不幸的是,我有一些困难,使我的基础设置工作。
我正在尝试实现与本教程类似的导航:教程
它或多或少是一个包含多个制表符的简单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>
SettingsActivity:
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:线程退出带有未明异常(group=0x41465700) 10-15 03:52:22.851: E/AndroidRuntime(889):致命异常:主10-15 03:52:22.851: E/AndroidRuntime(889):java.lang.IllegalStateException:您忘记调用“公共无效设置(LocalActivityManager ActivityGroup)”吗?10-1503:52:22.851: E/AndroidRuntime(889):at android.widget.TabHost$IntentContentStrategyandroid.widget.TabHost.setCurrentTab(TabHost.java:413) (TabHost.java:747) 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):在android.view.View.performClick(View.java:4240) 10-15 03:52:22.851: E/AndroidRuntime(889):android.view.View$PerformClick.run(View.java:17721) 10-15 :52:22.851: E/AndroidRuntime(889):android.os.Handler.handleCallback(Handler.java:730) 10-15 03:52:22.851:E/AndroidRuntime(889):在android.os.Handler.dispatchMessage(Handler.java:92) 10-15 03:52:22.851: E/AndroidRuntime(889):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 javajava.lang.reflect.Method.invoke(Method.java:525) (原生方法) 10-15 03:52:22.851: E/AndroidRuntime(889):在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$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(原生方法)
我认为问题在于,我的MainActivity不是从ActivityGroup扩展而来的,或者我没有使用LocalActivityManager。问题是,两者都不受欢迎。如果不使用不推荐的方法和类,我需要更改什么才能使它工作呢?
对于这个可能很简单的问题,我很抱歉,但我在google上什么也没发现,我对android程序很陌生:)。
发布于 2013-10-15 08:33:55
正如您所提到的,您应该从ActivityGroup扩展,但是如果您不想要一个不推荐的类,那么您可以使用Fragments
和FragmentManager
:片断
发布于 2013-10-15 08:33:20
尝尝这个
tabHost.addTab(tabHost.newTabSpec("Einstellungen").setIndicator("Einstellungen").setContent(R.id.tab2)));
像这样修改代码
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums)).setContent(intent);
tabHost.addTab(spec);
参见不错的Android教程示例hello-tabwidget
发布于 2013-10-15 08:50:13
做一些类似的事情,
TabHost tabHost = getTabHost();
Intent intentHome = new Intent().setClass(MainScreen.this, Home.class);
TabSpec tabSpecHome = tabHost.newTabSpec("Home").setIndicator("Home").setContent(intentHome);
tabHost.addTab(tabSpecHome);
tabHost.setCurrentTab(0);
希望这有帮助..。:)
https://stackoverflow.com/questions/19376473
复制相似问题