Android:添加一个简单的片段 [英] Android: Adding a simple Fragment

查看:197
本文介绍了Android:添加一个简单的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Android应用程序很新,所以我希望我能在这里找到一些帮助。
我已经在这里搜索了我的问题并找到了一些东西,但是这不起作用。

我想将一个片段添加到FrameLayout,但它确实不行。我的目标是创建一个始终存在的框架(/ Framework?),用户可以与它进行交互,并在此框架内的特定窗口中显示页面/片段,总共五个,并且可以切换在任何时候页面/片段,所以我有一个总是存在的框架,并在这个动态变化的页面。
但是现在我一开始就在这个框架中添加了一个简单的片段(它已经在btw中工作了)。

这些都是相关的代码我希望:
MainActivity.java发生错误(getSupportFragmentManager()。beginTransaction()。add(R.id.mainFrame,homeFragment).commit();)它告诉我:



lockquote

错误:(25,55)错误:找不到适合
的方法add(int,HomeFragment)方法FragmentTransaction.add(Fragment,String )
不适用(参数不匹配; int不能转换为
Fragment)方法FragmentTransaction.add(int,Fragment)不是
适用(参数不匹配; HomeFragment不能转换为
$ b


我已经尝试将homeFragment投射到Fragment,但这并不奏效。

MainActivity.java

  import android.app.FragmentTransaction; 
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.v4.app.FragmentActivity;


公共类MainActivity扩展FragmentActivity
{
FragmentTransaction fragmentTransaction;
HomeFragment homeFragment;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

homeFragment = new HomeFragment();
** getSupportFragmentManager()。beginTransaction()。add(R.id.mainFrame,homeFragment).commit(); **
}

activity_main.xml

 < FrameLayout xmlns:android = http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android.com/tools
android:id =@ + id /容器
android:layout_width =match_parent
android:layout_height =match_parent
工具:context =。MainActivity
工具:ignore =MergeRootFrame>

< FrameLayout
android:id =@ + id / mainFrame
android:layout_width =match_parent
android:layout_height =match_parent
android:layout_marginBottom =@ dimen / bottom_Main_Tabs>
< / FrameLayout>

[...]

< / FrameLayout>

fragment_home.xml

 < FrameLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android.com/tools
android:layout_width =match_parent
android:layout_height =match_parent
工具:context =com.example.HomeFragment> //它不是真的com.example ...

<! - TODO:更新空白片段布局 - >
< TextView
android:layout_width =match_parent
android:layout_height =match_parent
android:text =@ string / hello_blank_fragment/>

< / FrameLayout>

HomeFragment.java(所有东西都是自动生成的,但我已经剪掉了一些东西)

  public class HomeFragment extends Fragment 
{
private OnFragmentInteractionListener mListener;


public static HomeFragment newInstance()
{
HomeFragment fragment = new HomeFragment();
return fragment; //不是真正必要的,因为它缩短了它

$ b $ public HomeFragment()
{
//必需的空公共构造函数
}

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup容器,
Bundled savedInstanceState)
{
//膨胀布局对于这个片段
返回inflater.inflate(R.layout.fragment_home,container,false);
}

// TODO:将方法,更新参数和钩子方法重命名为UI事件
public void onButtonPressed(Uri uri)
{
if mListener!= null)
{
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
尝试
{
mListener =(OnFragmentInteractionListener)活动;

catch(ClassCastException e)
{
throw new ClassCastException(activity.toString()
+must implement OnFragmentInteractionListener);


$ b $ @Override
public void onDetach()
{
super.onDetach();
mListener = null;
}

public interface OnFragmentInteractionListener
{
// TODO:更新参数类型和名称
public void onFragmentInteraction(Uri uri);
}

}

有人可以帮我吗? / p>

John

解决方案

而新的类只适用于较新版本的操作系统。



例如,您导入 android.app.FragmentTransaction (可用于API 11+),但调用 getSupportFragmentManager()。beginTransaction()返回 android.support.v4.app.FragmentTransaction ...



您需要导入 android.support.v4.app.FragmentTransaction 和确保你的 HomeFragment 扩展 android.support.v4.app.Fragment 而不是 android .app.Fragment


I am pretty new to Android Apps, so I hope I can find some help here. I have already searched for my problem in here and found something, but that does not work.

I want to add a Fragment to a FrameLayout but it does not work. My goal ist to create a Frame (/Framework?) that is always present and the user can interact with it and inside this Frame in a specific "window" I want to display pages/fragments, five in total, and beeing able to switch the pages/fragments at any time, so I have an always present Frame and inside this dynamically changing pages. But for now I am stuck at the very beginning with adding a simple fragment to this Frame (which is already working btw.)

This is all the relevant code I hope: The error occurs in the MainActivity.java (getSupportFragmentManager().beginTransaction().add(R.id.mainFrame, homeFragment).commit();) where it tells me:

Error:(25, 55) error: no suitable method found for add(int,HomeFragment) method FragmentTransaction.add(Fragment,String) is not applicable (argument mismatch; int cannot be converted to Fragment) method FragmentTransaction.add(int,Fragment) is not applicable (argument mismatch; HomeFragment cannot be converted to Fragment)

I already tried to cast homeFragment to Fragment, but that did not work.

MainActivity.java

import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;


public class MainActivity extends FragmentActivity
{
    FragmentTransaction fragmentTransaction;
    HomeFragment homeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        homeFragment = new HomeFragment();
        **getSupportFragmentManager().beginTransaction().add(R.id.mainFrame, homeFragment).commit();**
}

activity_main.xml

<FrameLayout 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=".MainActivity"
    tools:ignore="MergeRootFrame" >

    <FrameLayout
        android:id = "@+id/mainFrame"
        android:layout_width = "match_parent"
        android:layout_height = "match_parent"
        android:layout_marginBottom = "@dimen/bottom_Main_Tabs">
        </FrameLayout>

    [...]

</FrameLayout>

fragment_home.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.HomeFragment"> // it is not really com.example...

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

HomeFragment.java (everything is autogenerated yet, but I already cut something out)

public class HomeFragment extends Fragment
{
    private OnFragmentInteractionListener mListener;


    public static HomeFragment newInstance()
    {
        HomeFragment fragment = new HomeFragment();
        return fragment; // not really neccessary, because it Have shortened it
    }

    public HomeFragment()
    {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri)
    {
        if (mListener != null)
        {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        try
        {
            mListener = (OnFragmentInteractionListener) activity;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach()
    {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener
    {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

}

Can somebody please help me?

John

解决方案

You're mixing up classes from the support library and the new classes available only to newer versions of the OS.

For example, you import android.app.FragmentTransaction (available for API 11+) but the call to getSupportFragmentManager().beginTransaction() returns android.support.v4.app.FragmentTransaction ...

You need to import android.support.v4.app.FragmentTransaction and make sure that your HomeFragment extends android.support.v4.app.Fragment and not android.app.Fragment.

这篇关于Android:添加一个简单的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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