Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android.support.v4.app.FragmentManager还是android.app.FragmentManager?_Android_Android Fragments_Fragment_Fragmentmanager - Fatal编程技术网

android.support.v4.app.FragmentManager还是android.app.FragmentManager?

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开发。我被一些本应该很容易的事情缠住了

我正在创建一个包含一个活动、两个片段和一个界面的应用程序

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中只需使用getSupportFragmentManager(),成功添加支持库后。

OP非常接近于拥有一个解决方案,该解决方案可以在不需要支持的情况下适用于API 11和更新的。v4

他只需要将其片段更改为在其导入语句中也不使用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
导入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.Fragmentsupport.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