gpt4 book ai didi

android - inflater.inflate 错误

转载 作者:行者123 更新时间:2023-11-30 03:10:32 25 4
gpt4 key购买 nike

我有一个 ListView ,每个项目加载一个 fragment ,其中包括另一个 fragment 。第一次充电没问题,但是当我返回到 lisview 并重新加载另一个 fragment (或相同 fragment )时,我在线上收到此错误:

view = inflater.inflate(R.layout.fragment_text_receiver, container, false);

错误:

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 的地方:

<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重复有问题?某种形式的“释放”?

编辑 1: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>

编辑 2: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;
}
}

}

第一次运行正常,但是当我回来尝试加载另一个时,出现了这个错误

最佳答案

解决方案是在框架父分离时强制删除子 fragment 。

@Override
public void onDetach() {
super.onDetach();
getActivity().getSupportFragmentManager().beginTransaction().remove(fragmentComments).commit();
}

关于android - inflater.inflate 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21066499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com