android.support.v4.app.FragmentManager或android.app.FragmentManager? [英] android.support.v4.app.FragmentManager OR android.app.FragmentManager?

查看:251
本文介绍了android.support.v4.app.FragmentManager或android.app.FragmentManager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习Android开发。我感到迷惑的东西,应该是很容易的。

林有一个活动,2碎片和1接口创建一个应用程序。

 安卓的minSdkVersion =11
机器人:targetSdkVersion =19

因此​​,在主要活动我试着去创建一个使用经理片段B的参考。我卡住在这里,因为Eclispse告诉我改变一些东西(见下文):

我的内涵:`

  @覆盖
    公共无效响应(int i)以{
        // TODO自动生成方法存根    FragmentManager经理= getFragmentManager();
    FragmentB F2 =(FragmentB)manager.findFragmentById(R.id.fragment2);}`

如果我这样做,这whay我得到错误信息,并需要进行一些改变。更改后的code看起来像这样(我仍然不能达到FragmentB):

  @覆盖
公共无效响应(int i)以{
    // TODO自动生成方法存根    android.app.FragmentManager经理= getFragmentManager();
    android.app.Fragment F2 = manager.findFragmentById(R.id.fragment2);}

有关额外的细节我还会把这里活动的导入头:

 包com.example.modular_ui;进口android.support.v7.app.ActionBarActivity;
进口android.support.v7.app.ActionBar;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentManager;
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.os.Build;公共类MainActivity扩展活动实现通讯{....

我缺少的是在这里吗?整个support.v4 /support.v7件事是新秀有点混乱。

编辑:
变化后:

 进口android.app.Fragment;
进口android.app.FragmentManager;

和延长FragmentActivity我还是不能创建FragmentB参考:

  @覆盖
公共无效响应(int i)以{
    // TODO自动生成方法存根FragmentManager人= getFragmentManager();
FragmentB B = man.findFragmentById(R.id.fragment2);

}

的要求,我已经张贴了FragmentB code:

 包com.example.modular_ui;进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.TextView;公共类FragmentB扩展片段{TextView的文本;@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
    // TODO自动生成方法存根
    返回inflater.inflate(R.layout.fragment_b,集装箱);
}@覆盖
    公共无效onActivityCreated(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onActivityCreated(savedInstanceState);
        文字=(TextView的)getActivity()findViewById(R.id.textView1)。
    }

主要XML

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID /容器
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=com.example.modular_ui.MainActivity
    工具:忽略=MergeRootFrame>    <片段
        机器人:ID =@ + ID /片段1
        机器人:名字=com.example.modular_ui.FragmentA
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT/>    <片段
        机器人:ID =@ + ID / fragment2
        机器人:名字=com.example.modular_ui.FragmentB
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID /片段1
        机器人:layout_marginTop =54dp/>< / RelativeLayout的>


解决方案

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

关于支持库。他们分别介绍了一些功能添加到旧的机器人。例如残片Android 3.0的(11 SDK NR)进行了介绍。事实上(根据文件)的机器人3.0<支持libary使用系统实施的片段。

I'm learning Android development. I get stuck at something that should be very easy.

Im creating an App with one Activity, 2 fragments and 1 interface.

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

So in the main activity Im trying to create a reference to Fragment B using the manager. I get stuck here, because Eclispse is telling me to change some things (see below):

My intension:`

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

    FragmentManager manager =getFragmentManager();
    FragmentB f2= (FragmentB) manager.findFragmentById(R.id.fragment2);

}`

If I do it this whay I get the error messages and need to perform some changes. After the changes the code looks like this (and I still can't reach 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);

}

For extra details I'll put here also the import header of the 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{....

What am I missing here? the whole support.v4 /support.v7 thing is a little confusing for rookies.

EDIT: After changing to:

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

AND extending FragmentActivity I still can't create a reference to FragmentB:

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

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

}

As Requested I've posted the FragmentB code:

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

Main 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>

解决方案

First of: your activity should extend FragmentActivity.

About support libraries. They were introduced to add some functionalities to older Androids. For example Fragments were introduced in Android 3.0 (SDK nr: 11). In fact (according to documentation) in Androids 3.0 < support libary uses system implementation of Fragments.

这篇关于android.support.v4.app.FragmentManager或android.app.FragmentManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆