我正在尝试构建结合了静态和动态元素的UI。为此,我将活动分为多个片段-所有应用程序的导航都通过替换片段而不是在活动之间进行导航来完成。
在我的主要活动布局中,我使用的是FrameLayout
:
<FrameLayout
android:id="@+id/mainframe"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="@id/topsection"
android:layout_above="@id/lowersection" />
我有一个这样声明的片段:
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragmentlayout, container, false);
}
}
然后,在我的主要活动(扩展FragmentActivity并使用import)中android.support.v4.app.FragmentActivity
,我试图将此片段加载到框架布局中。
MyFragment myf = new MyFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.mainframe, myf);
transaction.commit();
我在其他许多示例中都遵循了此步骤,但是我在该transaction.add()
命令上收到了一个编译器错误,似乎没有其他人遇到过。
我收到的错误是:The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment)
。
为什么是这样?在MyFragment
类扩展Fragment
,所以我会一直认为这是可行的。我究竟做错了什么?
编辑:我主要活动的导入是:
import org.joda.time.DateTime;
import android.app.FragmentTransaction;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
检查您的进口。使用android.support.v4.app.FragmentTransaction
代替android.app.FragmentTransaction
。
此外,请确保您正在使用android.support.v4.app.Fragment
和呼叫getSupportFragmentManager()
。很容易错过此调用/导入。在FragmentManager的提示下,对saiful103a表示感谢。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句