安卓1.6及以上片段&塔布主机
我正在升级一个 Android 应用程序(1.6 兼容性),它使用 TabHost
显示 3 个具有嵌套活动的不同选项卡。
当时我使用 ActivityGroup
技巧在选项卡中显示嵌套活动,但我对这种方法非常不满意,因为处理某些功能确实很痛苦。
我听说了 1.6 的 Fragments API 兼容性包,并且 Fragment
看起来非常适合我想要做的事情(在带有过渡效果和内容的选项卡中显示嵌套视图/功能),但我无法做到使用 TabHost
(它本来是要与 Action Bar
一起使用,但在兼容性包中不可用)。
你们中有人找到了在应用程序中创建这种结构的方法吗?
我的错误是:
错误/AndroidRuntime(955):导致:java.lang.RuntimeException:无法
开始活动 组件信息{com.XXX}: java.lang.IllegalArgumentException:否 找到 id 0x1020011 的视图 片段MyFragment
代码
main.xml
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
</LinearLayout>
</TabHost>
MainActivity.java
public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
final TabHost tabs = getTabHost();
TabHost.TabSpec spec;
Intent i;
i = new Intent(this, MyActivity.class);
spec = tabs.newTabSpec("MyActivity").setIndicator("MyActivity",res.getDrawable(R.drawable.tab)).setContent(i);
tabs.addTab(spec);
}
}
MyActivity.class
public class MyActivity extends FragmentActivity {
private static String TAG = "MyActivity";
private static FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ListeResultatFragment fragment = MyFragment.newInstance();
fragmentTransaction.add(android.R.id.tabcontent, fragment, "MyFragment");
fragmentTransaction.commit();
}
}
MyFragment.java
public class MyFragment extends Fragment {
public static MyFragment newInstance() {
MyFragment instance = new MyFragment();
return instance;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment, container, false);
}
}
I'm working on upgrading an Android application (1.6 compatibility) which uses a TabHost
to show 3 different tabs with nested activities.
At the time I used the ActivityGroup
trick to show nested activities in a tab but I'm very unhappy with this method since it's a real pain to handle some features.
I heard about the Fragments API compatibility package for 1.6 and a Fragment
looks perfect for what I want to do (show nested views / features within a tab with transition effects and stuff) but I can't make it work with a TabHost
(It was meant to work with an Action Bar
but it's not available in the compatibility package).
Did any of you guys found a way to create such a structure in your applications?
My error here is :
ERROR/AndroidRuntime(955): Caused by: java.lang.RuntimeException: Unable
to start activity
ComponentInfo{com.XXX}:
java.lang.IllegalArgumentException: No
view found for id 0x1020011 for
fragment MyFragment
CODE
main.xml
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
</LinearLayout>
</TabHost>
MainActivity.java
public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
final TabHost tabs = getTabHost();
TabHost.TabSpec spec;
Intent i;
i = new Intent(this, MyActivity.class);
spec = tabs.newTabSpec("MyActivity").setIndicator("MyActivity",res.getDrawable(R.drawable.tab)).setContent(i);
tabs.addTab(spec);
}
}
MyActivity.class
public class MyActivity extends FragmentActivity {
private static String TAG = "MyActivity";
private static FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ListeResultatFragment fragment = MyFragment.newInstance();
fragmentTransaction.add(android.R.id.tabcontent, fragment, "MyFragment");
fragmentTransaction.commit();
}
}
MyFragment.java
public class MyFragment extends Fragment {
public static MyFragment newInstance() {
MyFragment instance = new MyFragment();
return instance;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment, container, false);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您的
MyActivity
尝试使用android.R.id.tabcontent
作为容器 ID 来访问封闭的MainActivity
。这是不可能的。相反,MyActivity
需要有自己的布局(例如 FrameLayout),可以用作 Fragment 的父级。在这个布局中,必须存在一个可以通过id引用的视图。假设您有一个名为
activity_layout.xml
的布局,其中包含 ID 为“framelayout”的 FrameLayout。然后,您可以将MyActivity
中的onCreate
方法修改为如下所示:换句话说,
MyActivity
需要能够独立工作。首先尝试使其工作,然后将MyActivity
嵌入到包含 TabHost 的MainActivity
中。The problem is that your
MyActivity
tries to reach out to the enclosingMainActivity
by usingandroid.R.id.tabcontent
as a container id. This is not possible. Instead,MyActivity
needs to have its own layout (e.g. a FrameLayout) which can be used as the parent for the Fragment. In this layout, there must exist a view that can be referenced by id.Let's say you have a layout called
activity_layout.xml
which contains a FrameLayout with the id 'framelayout'. You can then modify theonCreate
method inMyActivity
to something like this:In other words,
MyActivity
needs to be able to work on its own. Try to make it work first, and then embedMyActivity
in theMainActivity
containing the TabHost.为什么不创建自己的标签栏。它非常容易构建。只需添加一个带有一些按钮的 LinearLayout 并设置 onClickListener 即可使用 FragmentManager 来切换片段。 Fragment 管理器可以从 FragmentActivity 中获取。
在 onClick 处理程序中,您只需执行一个事务即可切换到正确的片段。
Why don't you create your own tabbar. It's verry easy to build. Just add a LinearLayout with some buttons in it and set the onClickListener to switch fragments by using the FragmentManager. The Fragment manager can be obtained from a FragmentActivity.
In the onClick handler you just do a transaction to the switch to the correct fragments.