8

我正在学习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>
4

4 回答 4

10

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

于 2014-09-05T13:14:46.840 回答
7

OP 非常接近拥有一个无需 support.v4 即可适用于 API 11 及更新版本的解决方案。

他只需要将他的Fragment更改为在其import语句中也不使用 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

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

于 2015-09-21T14:31:00.530 回答
4

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

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

于 2014-04-13T16:01:19.963 回答
2

这很简单。

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

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

于 2017-01-21T14:26:52.983 回答