我对 Android 应用程序还很陌生,所以我希望能在这里找到一些帮助。
我已经在这里搜索了我的问题并找到了一些东西,但这不起作用。
我想将 Fragment 添加到 FrameLayout 但它不起作用。我的目标是创建一个始终存在的框架(/框架?),用户可以与它交互,并在这个框架内的特定“窗口”中我想显示页面/片段,总共五个,并且能够切换随时更改页面/片段,因此我有一个始终存在的框架并位于这个动态变化的页面内。
但现在我一开始就被困在向这个框架添加一个简单的片段(顺便说一句,它已经在工作了)。
这是我希望的所有相关代码:
错误发生在 MainActivity.java (getSupportFragmentManager().beginTransaction().add(R.id.mainFrame, homeFragment).commit();) 中,它告诉我:
错误:(25, 55) 错误:找不到合适的方法
添加(int,HomeFragment)方法 FragmentTransaction.add(Fragment,String)
不适用(参数不匹配;int 无法转换为
Fragment) 方法 FragmentTransaction.add(int,Fragment) 不是
适用(参数不匹配;HomeFragment 无法转换为
分段)
我已经尝试将 homeFragment 转换为 Fragment,但这不起作用。
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();**
}
活动主文件
<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>
片段_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(一切都是自动生成的,但我已经剪掉了一些东西)
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);
}
}
有人能帮帮我吗?
John