向活动添加片段。
扩展BaseActivity的我的活动:
@Bind(R.id.layout_frame_container) protected FrameLayout mFrameContainer;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_saved_searches_container);
if(null == savedInstanceState){
String screenTitle = getIntent().getExtras().getString(AppConstants.KEY_SCREEN_TITLE);
FCoreFragment savedSearchFragment = FSavedSearchesList.instance(screenTitle);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(mFrameContainer, savedSearchFragment).commit(); //I GET AN ERROR HERE CANT RESOLVE METHOD
}
}
FSavedSearchesList扩展了扩展Fragment(import android.support.v4.app.Fragment;
)
mFrameContainer
是FrameLayout ID。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/layout_frame_container">
</FrameLayout>
所以savedSearchFragment
应该是Fragment类型。
你能让我知道如何解决这个问题吗?
这是错误:
C:\Development\Android\TrendsFragment.java
Warning:(628, 36) non-varargs call of varargs method with inexact argument type for last parameter;
cast to Object for a varargs call
cast to Object[] for a non-varargs call and to suppress this warning
C:\Development\Android\salesiMobile\src\main\java\com\salesi\mobile\module\accounts\ui\container\SavedSearchesContainer.java
Error:(39, 32) error: no suitable method found for add(FrameLayout,FCoreFragment)
method FragmentTransaction.add(Fragment,String) is not applicable
(argument mismatch; FrameLayout cannot be converted to Fragment)
method FragmentTransaction.add(int,Fragment) is not applicable
(argument mismatch; FrameLayout cannot be converted to int)
Error:Execution failed for task ':salesiMobile:compileRefactorDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 18.121 secs
Information:2 errors
Information:1 warning
Information:See complete output in console
答案 0 :(得分:1)
您需要更新以下代码中的字段以调用任何片段。如果您想通过Instance方法调用您的片段,那么您可以在下面创建片段对象时更改它。
Fragment fragment = new Fragment();
if (arguments != null) {
fragment.setArguments(arguments);
arguments = null;
}
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
android.R.anim.fade_out);
fragmentTransaction.replace(R.id.frame, fragment, arguments);
fragmentTransaction.commit();
答案 1 :(得分:0)
您需要使用getSupportFragmentManager()
来支持片段,因为您使用android.support.v4.app.Fragment
并使用R.id.layout_frame_container
的FrameLayout的ID:
import android.support.v4.app.FragmentTransaction;
...
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.layout_frame_container, savedSearchFragment)
.commit();
答案 2 :(得分:0)
添加片段fragmentTransaction.add(mFrameContainer, savedSearchFragment).commit();
通常需要容器ID而不是引用。
将mFrameContainer
更改为R.id.layout_frame_container
,它应该有效。
此外,由于您使用的是android.support.v4.app.Fragment,因此将管理员从getFragmentManager()
更改为getSupportFragmentManager().
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
答案 3 :(得分:0)
创建容器
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_marginBottom="56dp"
android:layout_height="match_parent" />
使用此MainActivity.java类定义片段管理器和transection globel
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
ft.add(R.id.container, new Home_Fragment(),
"Home_Fragment").commit();
答案 4 :(得分:0)
在xml文件中创建FrameLayout容器。
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
打开你的java类并使用事务定义片段管理器。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Your_Fragment your_Fragment=new Your_Fragment();
replaceFragment(your_Fragment);
}
public void replaceFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.commit();
}