【问题标题】:android.support.v4.app.FragmentManager OR android.app.FragmentManager?android.support.v4.app.FragmentManager 还是 android.app.FragmentManager?
【发布时间】:2014-05-27 12:17:00
【问题描述】:

我正在学习 Android 开发。我被一些应该很容易的事情困住了。

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

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):

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

}

关于更多细节,我还将在此处放置 Activity 的导入标头:

  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

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>

【问题讨论】:

  • 支持库主要是为了支持 ActionBar 和其他一些直到 API 11 才支持的关键特性。如果你的最小 API 为 11,我会使用常规库来避免添加不需要的批量。除非有什么我不知道的。
  • 感谢您的快速回复。 Eclipse 自动实现支持库。所以你建议把它改成: import android.app.Fragment;导入 android.app.FragmentManager;
  • 您可以使用其中任何一个,但我认为如果您的 API 高于 11 或更高,我认为没有必要,因为此时本机库支持所有(我所知道的) .如果是这种情况,它只是在您的应用中占据大量内容,并且可能会导致您在试图记住您正在使用的内容时感到头疼。

标签: android android-fragments fragment fragmentmanager


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    OP 非常接近于拥有一个可以在 API 11 和更新版本无需支持的情况下正常工作的解决方案。v4

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

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

    支持-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
    

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

    【讨论】:

      【解决方案3】:

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

      关于支持库。引入它们是为了向较旧的 Android 添加一些功能。例如,Android 3.0 (SDK nr: 11) 中引入了 Fragments。事实上(根据文档)在 Androids 3.0

      【讨论】:

      • 你能发布你的代码来创建和附加 FragmentB 吗?主要活动 xml 也会有所帮助。
      • 我在下面添加了这些东西
      • 您的片段扩展了支持库的片段。更改导入android.support.v4.app.Fragment;导入android.app.Fragment;在片段B中
      • 过时的答案。因为 OP 的目标是 API 11 和更新版本,所以不需要支持 v4 的东西,因此不需要 FragmentActivity(这是支持 v4 的一部分)。正如 RobertM 所说,真正的 问题是他的Fragment 做了import android.support.v4.app.Fragment; 从而使他采用了这种旧方法。删除所有对support.v4 的引用。只需说import android.app.Fragment; 使用来自developer.android 或其他地方的现代片段示例,在其中的任何地方都没有说support.v4。然后你可以简单地说getFragmentManager()
      【解决方案4】:

      很简单。

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-09
        相关资源
        最近更新 更多