android.support.v4.app.FragmentManager还是android.app.FragmentManager?
我正在学习Android开发。我被一些本应该很容易的事情缠住了 我正在创建一个包含一个活动、两个片段和一个界面的应用程序android.support.v4.app.FragmentManager还是android.app.FragmentManager?,android,android-fragments,fragment,fragmentmanager,Android,Android Fragments,Fragment,Fragmentmanager,我正在学习Android开发。我被一些本应该很容易的事情缠住了 我正在创建一个包含一个活动、两个片段和一个界面的应用程序 android:minSdkVersion="11" android:targetSdkVersion="19 因此,在主要活动中,我试图使用管理器创建对片段B的引用。我被困在这里,因为Eclispse告诉我要改变一些事情(见下文): 我的意图是:` @Override public void respond(int i) { // TODO Aut
android:minSdkVersion="11"
android:targetSdkVersion="19
因此,在主要活动中,我试图使用管理器创建对片段B的引用。我被困在这里,因为Eclispse告诉我要改变一些事情(见下文):
我的意图是:`
@Override
public void respond(int i) {
// TODO Auto-generated method stub
FragmentManager manager =getFragmentManager();
FragmentB f2= (FragmentB) manager.findFragmentById(R.id.fragment2);
}`
如果我这样做,我会收到错误消息,需要执行一些更改。更改后的代码如下所示(我仍然无法访问FragmentB):
有关更多详细信息,我还将在此处显示活动的导入标题:
package com.example.modular_ui;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends Activity implements Communicator{....
我错过了什么?对于新手来说,整个support.v4/support.v7有点让人困惑
编辑:
更改为后:
import android.app.Fragment;
import android.app.FragmentManager;
扩展FragmentActivity我仍然无法创建对FragmentB的引用:
@Override
public void respond(int i) {
// TODO Auto-generated method stub
android.app.FragmentManager manager =getFragmentManager();
android.app.Fragment f2= manager.findFragmentById(R.id.fragment2);
}
@Override
public void respond(int i) {
// TODO Auto-generated method stub
FragmentManager man = getFragmentManager();
FragmentB b = man.findFragmentById(R.id.fragment2);
}
根据要求,我发布了FragmentB代码:
package com.example.modular_ui;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FragmentB extends Fragment {
TextView text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_b, container);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
text = (TextView) getActivity().findViewById(R.id.textView1);
}
主XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.modular_ui.MainActivity"
tools:ignore="MergeRootFrame" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.modular_ui.FragmentA"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.modular_ui.FragmentB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/fragment1"
android:layout_marginTop="54dp" />
</RelativeLayout>
首先:您的活动应该扩展FragmentActivity
关于支持库。引入它们是为了给较老的机器人增加一些功能。例如,Android 3.0(SDK编号:11)中引入了片段。事实上(根据文档),在Androids 3.0中
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment; <-- ".support.v4"
import android.support.v4.app.FragmentManager;
... MainActivity extends FragmentActivity ...
... = getSupportFragmentManager();
.... YourFragment extends Fragment ... <-- same in both approaches
导入android.support.v4.app.FragmentActivity;
导入android.support.v4.app.Fragment 很简单
如果您还希望应用程序在较旧的设备上运行(低于API级别11),请使用getSupportFragmentManager()
如果您希望应用程序在API级别高于11的设备中运行,请使用
getFragmentManger()
支持库主要用于支持ActionBar和一些在API 11之前不受支持的其他关键功能。如果你的最小API为11,我会使用常规库来避免添加不必要的批量。除非有什么我不知道的。谢谢你的快速回复。Eclipse自动实现支持库。因此,您建议将其更改为:import android.app.Fragment;导入android.app.FragmentManager;您可以使用其中任何一种,但我认为如果您的API高于11或更高,那么就没有必要了,因为在这一点上,所有(我所知道的)东西都是由本机库支持的。如果是这样的话,它在你的应用程序中只是一个大容量的东西,如果你想记住你正在使用的是哪一个,可能会让人头疼。你能发布你创建和附加FragmentB的代码吗?主活动xml也会有帮助。我已经在下面添加了这些东西,您的片段扩展了支持库的片段。更改导入android.support.v4.app.Fragment;导入android.app.Fragment;支离破碎的回答。因为OP的目标是API 11和更新版本,不需要support v4的内容,因此不需要FragmentActivity(这是support v4的一部分)。正如RobertM所说,真正的问题是他的片段导入了android.support.v4.app.Fragment从而使他坚持这种老方法。删除对support.v4
的所有引用。只需说导入android.app.Fragment代码>使用developer.android或其他地方的一个现代片段示例,其中没有说支持.v4
。然后您可以简单地说getFragmentManager()
。
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
... MainActivity extends Activity ...
... = getFragmentManager();
.... YourFragment extends Fragment ... <-- same in both approaches