In my MainActivity I have a fragment which I replace by an other fragment class.
Replace fragment in MainActivity:
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, MainFragment.newInstance())
.commit();
XML of MainActivity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:titleTextColor="@android:color/white" />
</android.support.design.widget.AppBarLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</FrameLayout>
This works fine.
The problem is now in my Fragment class. I want to call a new Activty (DetailRfrdPastActivity) from the fragment class. As soon as I add an element to the DetailRfrdPastActivity XML the app stops with the following error:
java.lang.IllegalArgumentException: No view found for id 0x7f0c0071 (com.scherrer.robin.chvote:id/touch_outside) for fragment MainFragment{610d48e #0 id=0x7f0c0071} at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1098) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
In the fragment class I start the activity like this:
public class MainFragment extends Fragment implements SearchView.OnQueryTextListener, FlexibleAdapter.OnItemClickListener {
public static MainFragment newInstance() {
return new MainFragment();
}
private RecyclerView mRecyclerView;
private FlexibleAdapter<IFlexible> mAdapter;
private List<IFlexible> originalItems;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_main, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.lvRef);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
this.mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
originalItems = AdapterData.getRfrdList();
mAdapter = new FlexibleAdapter<>(AdapterData.getRfrdList());
mAdapter.showAllHeaders();
mAdapter.initializeListeners(this);
mRecyclerView.setAdapter(mAdapter);
mAdapter.enableStickyHeaders();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
}
@Override
public boolean onQueryTextChange(String query) {
List<IFlexible> copyOriginalItems = new ArrayList<>(originalItems);
mAdapter.setSearchText(query);
mAdapter.filterItems(copyOriginalItems);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onItemClick(int itemPosition) {
IFlexible clickedItem = mAdapter.getItem(itemPosition);
if (clickedItem instanceof ItemRfrdPast) {
Intent detailRfrdPast = new Intent(getActivity().getApplicationContext(), DetailRfrdPastActivity.class);
detailRfrdPast.putExtra("pastRfrd", ((ItemRfrdPast) clickedItem).getPastRfrd());
getActivity().startActivity(detailRfrdPast);
}
return true;
}
}
XML of Fragment:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/lvRef"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
<include layout="@layout/sticky_header_layout"/>
The DetailRfrdPastActivity is the child activity of MainActivity. I can't figure out where the error is because the fragment and the called activity should have nothing to do with each other.
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activities.MainActivity"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.DetailRfrdPastActivity"
android:parentActivityName=".activities.MainActivity" >
</activity>
</application>
getSupportFragmentManager().beginTransaction()
withgetChildFragmentManager().beginTransaction()
. – Shree Krishna