在我的MainActivity中,我有一个片段,我用另一个片段类替换它。
替换MainActivity:中的碎片
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, MainFragment.newInstance())
.commit();
主活动的XML:
<?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>
这很好用。
这个问题现在在我的Fragment类中。我想从fragment类中调用一个新的Activty(DetailRfrdPastActivity)。一旦我向DetailRfrdPastActivity XML添加元素,应用程序就会停止,并出现以下错误:
java.lang.IollegalArgumentException:找不到id 0x7f0c0071的视图(com.scherrer.robin.chvote:id/touch_outside)用于片段主片段{610d48e#0 id=0x7f0c0071}在android.support.v4.app.FFragmentManagerImpl.moveToState(FragmentManager.java:1098)在android.support.v4.app.FFragmentManagerImpl.moveToState(FragmentManager.java:1286)在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)在android.support.v4.app.FragageManagerImpl.execPendingActions(FragmentManager.java:1671)在android.support.v4.app.FFragmentManagerImpl$1.run(FragmentManager.java:532)在android.os.Handler.handleCallback(Handler.java:739)在android.os.Handler.dispatchMessage(Handler.java:95)在android.os.Looper.loop(Looper.java:148)在android.app.ActivityThread.main(ActivityThreads.java:5417)位于java.lang.reflect.Method.ioke(本机方法)在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:616)
在片段类中,我开始这样的活动:
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:
<?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"/>
DetailRfrdPastActivity是MainActivity的子活动。我不知道错误在哪里,因为片段和调用的活动应该没有任何关系。
清单:
<?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>
ftw,我认为从ViewHolder设置onClickListener并用所选项目调用回调会很好。
例如:
来自FlexibleAdapter类
public interface OnIFlexibleClickListener {
void onIFlexibleClick(IFlexible iFlexible);
}
private OnIFlexibleClickListener listener;
public setOnIFlexibleClickListener(OnIFlexibleClickListener listener) {
this.listener = listener;
}
public IFlexible getIFlexible(int position) {
return iFlexibleList.get(position); //Your iFlexible List
}
public class FindViewHolder extends RecyclerView.ViewHolder {
private TextView textView; //Replace the TextView with your own view.
public FindViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView;
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onIFlexibleClick(getIFlexible(getAdapterPosition()));
}
}
});
}
}
从MainFragment类实现OnIFlexibleClickListener
@Override
public void onIFlexibleClick(IFlexible iFlexible) {
if (iFlexible instanceof ItemRfrdPast) {
ItemRfrdPast itemRfrdPast = ((ItemRfrdPast) iFlexible).getPastRfrd()
Intent intent = new Intent(getContext(), DetailRfrdPastActivity.class);
intent.putExtra(PAST_RFRD, itemRfrdPast); //public static final String PAST_RFRD = "pastRfrd";
getActivity().startActivityForResult(intent, DetailRfrdPastActivity.REQUEST_CODE); //create a public static final int REQUEST_CODE
}
}
我认为这应该能解决问题。干杯。
显示MainFragment xml文件中缺少id:id/touch_outside的视图。请在您的xml以及包含的xml 中检查此id
替换
getSupportFragmentManager().beginTransaction()
带有
getChildFragmentManager().beginTransaction()
为什么会这样
可能是因为它找不到具有给定ID的片段,因为它在不同的
FragmentManager
中。
参考差异