我正在创建一个显示视频列表(VideoListingActivity)的活动,具体取决于从父activity.my传递的参数。我试图将传递给VideoListingActivity的包传递给它的子片段,以便它可以获取相关数据从数据库中显示它在活动上。我试图使用Fragment.setArguments()将包传递给片段,但得到以下错误。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rrdtech.vidyavaan.android/com.rrdtech.vidyavaan.android.VideoListingActivity}: java.lang.IllegalStateException: Fragment already active
我认为片段附加到onCreate()的活动帖子,所以甚至会引发这个错误吗?
以下是我的活动和片段的代码
VideoListingActivity.java
public class VideoListingActivity extends ActionBarActivity implements FilterFragment.OnFilterItemSelectedListener,
android.support.v7.widget.SearchView.OnQueryTextListener
{
public VideoListFragment videoListFragment;
private android.support.v7.widget.SearchView searchView;
private Context context;
private Bundle bundle;
private String listBy;
private String subjectId;
private String levelType;
private FragmentTransaction ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_listing);
// videoListFragment=new VideoListFragment();
videoListFragment= (VideoListFragment) getFragmentManager().findFragmentById(R.id.frg_video_listing_video_listing_fragment);
ft =getFragmentManager().beginTransaction();
context=this;
bundle = new Bundle();
bundle = getIntent().getExtras();
videoListFragment.setArguments(bundle);
}
@Override
protected void onStart() {
super.onStart();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_video_listing, menu);
MenuItem menuItem=menu.findItem(R.id.item_action_search);
//SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (android.support.v7.widget.SearchView) menu.findItem(R.id.item_action_search).getActionView();
setupSearchView(menuItem);
//searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
public void setupSearchView(MenuItem searchItem)
{
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if(searchManager != null)
{
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
searchView.setSearchableInfo(info);
}
searchView.setOnQueryTextListener(this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onFilterItemSelected(String filterId) {
videoListFragment.filterSelectedAction("filter id is "+filterId);
}
@Override
public boolean onQueryTextSubmit(String query) {
CommonFunctions commonFunctions = new CommonFunctions();
commonFunctions.showProgressDialog(context,"getting data","bringing");
ft.replace(R.id.frg_video_listing_filter_fragment,videoListFragment);
ft.commit();
commonFunctions.dismissProgressDialog();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
}
VideoListingFragment.java
public class VideoListFragment extends Fragment {
private ListView videoListView;
private Context activityContext;
private VideosListAdapter listAdapter;
private List<Video> videosList;
private Bundle b;
private String listBy;
private String subjectId;
private String levelType;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
activityContext = activity.getApplicationContext();
b=new Bundle();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.video_list_fragment,container,false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
b= getArguments();
listBy = b.getString(VidyavaanGlobal.listBy);
Log.d("list by ",listBy);
}
@Override
public void onStart() {
super.onStart();
}
public void filterSelectedAction(String x)
{
Log.d("passed is ", x);
}
public void fillData()
{
videosList.add(new Video("chapter1","title1", "pragyan public school", "pawan saraskar", "Chemistry", "Advance", "abcd url", "21", "5/6/2015", "thumbnail url"));
videosList.add(new Video("chapter2","title2","pragyan public school","pawan saraskar","physics","Advance","abcd url","21","5/6/2015","thumbnail url"));
videosList.add(new Video("chapter3","title3","pragyan public school","pawan saraskar","english","Advance","abcd url","21","5/6/2015","thumbnail url"));
videosList.add(new Video("chapter4","title4","pragyan public school","pawan saraskar","Chemistry","Advance","abcd url","21","5/6/2015","thumbnail url"));
videosList.add(new Video("chapter5","title5","pragyan public school","pawan saraskar","Chemistry","Advance","abcd url","21","5/6/2015","thumbnail url"));
videosList.add(new Video("chapter6","title6","pragyan public school","pawan saraskar","Chemistry","Advance","abcd url","21","5/6/2015","thumbnail url"));
videosList.add(new Video("chapter7", "title7", "pragyan public school", "pawan saraskar", "Chemistry", "Advance", "abcd url", "21", "5/6/2015", "thumbnail url"));
}
public void displayVideoList(boolean searchReqeust )
{
}
public void displayVideoList(String pListBy, String pValue)
{
listAdapter = new VideosListAdapter(activityContext,videosList);
videoListView.setAdapter(listAdapter);
}
public void displayVideoList(String subjectId, String videoType, String VideoLevel)
{
}
}
可以请一位帮忙。提前谢谢。
答案 0 :(得分:2)
来自官方Android开发参考:
Public void setArguments(Bundle args)提供构造 这个片段的参数。这只能在之前调用 片段已附加到其活动;也就是说,你应该打电话 它在构建片段后立即生效。提供的论据 这里将保留片段销毁和创建。
您的片段已附加到其活动中我建议您使用自己的方法,不需要@Override
public void onBackPressed() {
Log.d("back", "onBackPressed Called");
// Do your stuff here
}
!
在片段类中创建自己的setArguments
并更新此方法中的片段UI
您将以这种方式调用此方法:
setUIArguments(Bundle args)
片段类 中的
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Detailfragment detailFragment = (Detailfragment)getFragmentManager().findFragmentById(detailFragmentID);
Bundle bundle = new Bundle();
bundle.putSerializable(BUNDLE_KEY, obj);// passing this object
detailFragment.setUIArguments(bundle); /* your new method */
}
答案 1 :(得分:0)
致电
bundle = new Bundle();
bundle = getIntent().getExtras();
videoListFragment.setArguments(bundle);
之前
ft =getFragmentManager().beginTransaction();
线。
答案 2 :(得分:0)
在添加bundle作为参数后尝试调用ft =getFragmentManager().beginTransaction();
。