【发布时间】:2012-07-23 19:58:05
【问题描述】:
我正在使用来自 reference. 的代码
当我在我的程序中输入该代码时,出现如下图所示的错误。
错误的任何原因?
The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, ExampleFragments)
我的主要活动中的代码:
public void red(View view) {
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragments fragment = new ExampleFragments();
fragmentTransaction.replace(R.id.frag, fragment);
fragmentTransaction.commit();
}
ExampleFragments.java
package com.example.learn.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ExampleFragments extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.blue_pill_frag, container, false);
}
}
这里:
package com.example.learn.fragments;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
【问题讨论】:
-
你能展示你的
ExampleFragments类吗?我怀疑它不会扩展Fragment。 -
更新了我的问题以显示 ExampleFragments.java
-
你为什么用
ExampleFragments fragment = new ExampleFragments();而不是Fragment fragment = new Fragment()? -
我从developer.android.com/guide/components/fragments.html
You can then add a fragment using the add() method, specifying the fragment to add and the view in which to insert it. For example:复制粘贴 -
澄清一下,为了消除错误,您必须使 Android 的示例 FragmentLayout.java 代码的“DetailsActivity”扩展“FragmentActivity”而不是“Activity”,然后 Eric 的解决方案将起作用。
标签: java android android-fragments