android.support.v4.app.FragmentManager或android.app.FragmentManager?

时间:2014-04-13 15:54:01

标签: android android-fragments fragment fragmentmanager

我正在学习Android开发。我陷入了一件非常容易的事情。

我正在创建一个包含一个Activity,2个片段和一个界面的应用程序。

android:minSdkVersion="11"
android:targetSdkVersion="19

所以在主要活动中我试图使用管理器创建对Fragment 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):

    @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);

}

有关额外的详细信息,我还会在此处放置活动的导入标题:

  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;

AND扩展FragmentActivity我仍然无法创建对FragmentB的引用:

@Override
public void respond(int i) {
    // TODO Auto-generated method stub

FragmentManager man = getFragmentManager();
FragmentB b = man.findFragmentById(R.id.fragment2);

}

As Requested我发布了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>

4 个答案:

答案 0 :(得分:9)

只需使用getSupportFragmentManager(); ,成功添加支持库之后。

答案 1 :(得分:6)

OP非常接近于提供一个适用于API 11和更新 的解决方案,而无需support.v4

他只需要在 import 语句中更改 Fragment ,也不要使用support.v4。

两种方法的总结。 您的所有活动和片段应该包含看起来完全一个的代码;不要混合它们! (并非所有文件都需要所有行;根据需要使用行。)

support-v4方法

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

API 11+方法

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;

... MainActivity extends Activity ...

... = getFragmentManager();

.... YourFragment extends Fragment ... <-- same in both approaches

因此,如果您有一个使用上述方法编写的项目,并且您正在集成其他地方的代码,请务必查找这些行,并将其更改为与您拥有的相匹配。

答案 2 :(得分:4)

首先:您的活动应该扩展FragmentActivity。

关于支持库。引入它们是为了给老式机器人添加一些功能。例如,片段是在Android 3.0(SDK nr:11)中引入的。事实上(根据文件)Androids 3.0&lt;支持libary使用Fragments的系统实现。

答案 3 :(得分:2)

很简单。

如果您还希望自己的应用在较旧的设备上运行(低于API级别11),请使用getSupportFragmentManager()

如果您希望自己的应用在API级别大于11的设备上运行,请使用 getFragmentManger()