我有一个列表视图,每个项目加载一个片段,其中包含另一个片段。第一次收费很好,但当我返回lisview并重新加载另一个片段(或相同)时,我收到了这个错误:
view = inflater.inflate(R.layout.fragment_text_receiver, container, false);
ERROR:
01-11 15:40:59.940: E/AndroidRuntime(14700): FATAL EXCEPTION: main
01-11 15:40:59.940: E/AndroidRuntime(14700): android.view.InflateException: Binary XML file line #43: Error inflating class fragment
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-11 15:40:59.940: E/AndroidRuntime(14700): at com.gzanitti.tsar.fragments.ReceiverFragment.onCreateView(ReceiverFragment.java:70)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.os.Handler.handleCallback(Handler.java:615)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.os.Handler.dispatchMessage(Handler.java:92)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.os.Looper.loop(Looper.java:137)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.app.ActivityThread.main(ActivityThread.java:4898)
01-11 15:40:59.940: E/AndroidRuntime(14700): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 15:40:59.940: E/AndroidRuntime(14700): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 15:40:59.940: E/AndroidRuntime(14700): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-11 15:40:59.940: E/AndroidRuntime(14700): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-11 15:40:59.940: E/AndroidRuntime(14700): at dalvik.system.NativeStart.main(Native Method)
01-11 15:40:59.940: E/AndroidRuntime(14700): Caused by: java.lang.IllegalArgumentException: Binary XML file line #43: Duplicate id 0x7f050069, tag null, or parent id 0x0 with another fragment for com.gzanitti.tsar.fragments.CommentsFragment
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
01-11 15:40:59.940: E/AndroidRuntime(14700): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
01-11 15:40:59.940: E/AndroidRuntime(14700): ... 21 more
我不知道会发生什么。 XML文件行34是声明片段的地方:
<fragment
android:id="@+id/fragmentComments"
android:name="com.gzanitti.tsar.fragments.CommentsFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtReceiverText"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/txtReceiverText"
android:layout_alignTop="@+id/txtReceiverTitle"/>
重复ID可能有些问题?某种形式的“释放”?
EDIT1:fragment_text_receiver.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
tools:context=".ReceiverActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<EditText
android:id="@+id/txtReceiverTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="@string/insertTitle" />
<EditText
android:id="@+id/txtReceiverText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/txtReceiverTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:ems="10"
android:gravity="center_vertical|top"
android:hint="@string/shareText"
android:inputType="textMultiLine" />
<fragment
android:id="@+id/fragmentComments"
android:name="com.gzanitti.tsar.fragments.CommentsFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtReceiverText"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/txtReceiverText"
android:layout_alignTop="@+id/txtReceiverTitle"/>
</RelativeLayout>
<FrameLayout
android:id="@+id/frameComments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignWithParentIfMissing="true" >
<TextView
android:id="@+id/txtShowComments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center"
android:text="@string/showComments"
android:textAlignment="center" />
</FrameLayout>
</RelativeLayout>
</ScrollView>
EDIT2:receiverFragment.java
public class ReceiverFragment extends Fragment {
public static final int RECEIVER_FRAGMENT = 5;
public static String s3Name;
public static int post_id;
public static String title;
public static String text;
public static int type;
public static File filePath;
protected MediaPlayer mPlayer;
protected View view;
public CommentsFragment fragmentComments;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SesionActivity.actualFragment = RECEIVER_FRAGMENT;
setHasOptionsMenu(true);
title = getArguments().getString("notifTitle");
text = getArguments().getString("notifText");
post_id = getArguments().getInt("notifPostID");
CommentsFragment.post_id = post_id;
type = getArguments().getInt("notifType");
s3Name = getArguments().getString("s3Name");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_text_receiver, container, false);
EditText txtTextReceiverTitle = (EditText) view.findViewById(R.id.txtReceiverTitle);
txtTextReceiverTitle.setKeyListener(null);
txtTextReceiverTitle.setText(title);
EditText txtTextReceiver = (EditText) view.findViewById(R.id.txtReceiverText);
txtTextReceiver.setKeyListener(null);
txtTextReceiver.setText(text);
FrameLayout frameComments = (FrameLayout) view.findViewById(R.id.frameComments);
frameComments.setOnClickListener(new OnClickReceiverFragmentListener(getActivity(), this));
FragmentManager fManager = getActivity().getSupportFragmentManager();
fragmentComments = (CommentsFragment) fManager.findFragmentById(R.id.fragmentComments);
fManager.beginTransaction().hide(fragmentComments).commit();
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater().inflate(R.menu.receiver, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.btnSpread:
spreadData();
return true;
case R.id.btnCancel:
deleteInSQL();
backToPendingFragment();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void spreadData() {
final SesionActivity sActivity = (SesionActivity) getActivity();
sActivity.pDialog = new ProgressDialog(getActivity());
sActivity.pDialog.setTitle(getText(R.string.spreading));
sActivity.pDialog.setMessage(getText(R.string.wait));
sActivity.pDialog.setCancelable(false);
sActivity.pDialog.show();
new Thread(new Runnable() {
@Override
public void run() {
TsarServer tServer = new TsarServer(sActivity);
boolean result = tServer.spread(Integer.toString(ReceiverFragment.post_id), ReceiverFragment.title, ReceiverFragment.text,
ReceiverFragment.type, ReceiverFragment.s3Name);
Message msg = new Message();
msg.obj = result;
puenteReceiver.sendMessage(msg);
}
}).start();
}
private Handler puenteReceiver = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
boolean resPost = (Boolean) msg.obj;
((SesionActivity)getActivity()).pDialog.dismiss();
if(resPost) {
Toast.makeText(getActivity(), R.string.okPost, Toast.LENGTH_LONG).show();
deleteSource();
deleteInSQL();
backToPendingFragment();
} else {
Toast.makeText(getActivity(), R.string.errorPost, Toast.LENGTH_LONG).show();
}
return true;
}
});
private void deleteSource() {
if(ReceiverFragment.filePath != null) {
File file = new File(ReceiverFragment.filePath.toString());
file.delete();
}
}
private void deleteInSQL() {
PendingSQL sql = new PendingSQL(getActivity(), "dbPending", null, 1);
SQLiteDatabase db = sql.getWritableDatabase();
String[] args = new String[]{Integer.toString(ReceiverFragment.post_id)};
db.delete("pending", "postID=?", args);
}
private void backToPendingFragment() {
PendingFragment pendingFragment = new PendingFragment();
FragmentManager fManager = getActivity().getSupportFragmentManager();
fManager.popBackStack();
fManager.beginTransaction().replace(R.id.content_frame, pendingFragment).commit();
}
}
类OnClickReceiverFragmentListener实现OnClickListener {
private FragmentActivity mActivity;
private ReceiverFragment mFragment;
public OnClickReceiverFragmentListener(FragmentActivity activity, ReceiverFragment fragment) {
this.mActivity = activity;
this.mFragment = fragment;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnPlay:
ImageButton btnPlay = ((ImageButton) mActivity.findViewById(R.id.btnPlay));
if(btnPlay.isEnabled()) {
mFragment.mPlayer.start();
}
break;
case R.id.btnPause:
ImageButton btnPause = ((ImageButton) mActivity.findViewById(R.id.btnPause));
if(btnPause.isEnabled()) {
mFragment.mPlayer.pause();
}
break;
case R.id.frameComments:
TextView txtComments = (TextView) v.findViewById(R.id.txtShowComments);
FragmentManager fManager = mFragment.getFragmentManager();
if(CommentsFragment.isOpen) {
CommentsFragment.isOpen = false;
txtComments.setText("Show comments");
fManager.beginTransaction()
.setCustomAnimations(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom)
.hide(mFragment.fragmentComments)
.commit();
} else {
CommentsFragment.isOpen = true;
txtComments.setText("Hide comments");
fManager.beginTransaction()
.setCustomAnimations(R.anim.abc_slide_in_bottom, R.anim.abc_slide_out_bottom)
.show(mFragment.fragmentComments)
.commit();
}
break;
default:
break;
}
}
}
第一次工作正常,但当我回来并尝试加载另一个时,会发生此错误
答案 0 :(得分:1)
解决方案是强制将子片段移除到frament parent detach上。
@Override
public void onDetach() {
super.onDetach();
getActivity().getSupportFragmentManager().beginTransaction().remove(fragmentComments).commit();
}