一切都很完美,但如果你第二次使用fragment2按钮,应用程序就会停止。据我了解日志,这个片段已经打开,或者在这个片段中已经打开了另一个片段。 已经超出我的帮助
Main2activity:
case R.id.inbox:
Toast.makeText(getApplicationContext(),"Inbox Selected",Toast.LENGTH_SHORT).show();
ContentFragment fragment = new ContentFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.remove(fragment);
fragmentTransaction.replace(R.id.frame,fragment);
fragmentTransaction.commit();
return true;
// For rest of the options we just show a toast on click
case R.id.starred:
Toast.makeText(getApplicationContext(),"Stared Selected",Toast.LENGTH_SHORT).show();
ListVideoViewFragment fragment2 = new ListVideoViewFragment();
FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
fragmentTransaction2.addToBackStack(null);
fragmentTransaction2.remove(fragment2);
fragmentTransaction2.replace(R.id.frame,fragment2);
fragmentTransaction2.commit();
return true;
case R.id.sent_mail:
Toast.makeText(getApplicationContext(),"Send Selected",Toast.LENGTH_SHORT).show();
return true;
Listviewfragment:
public class ListVideoViewFragment extends Fragment implements VideosFragment.Contract {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_list_video_view_fragment,container,false);
return v;
}
@Override
public void onVideoSelected(final String uri, String mimeType) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Select");
// builder.setMessage("Lorem ipsum dolor ....");
builder.setItems(new CharSequence[]
{getString(R.string.play_video), getString(R.string.remove_video)},
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
switch (which) {
case 0:
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra("url", uri);
startActivity(intent);
break;
case 1:
File file = new File(uri);
file.delete();
getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(uri))));
//sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(new File(uri))));
// Main2Activity.this.getContentResolver().delete(Uri.fromFile(new File(uri)), null,null);
break;
}
}
});
builder.create().show();
}
}
和videofragment:
public class VideosFragment extends ContractListFragment<VideosFragment.Contract> implements LoaderManager.LoaderCallbacks<Cursor>, SimpleCursorAdapter.ViewBinder {
private ImageLoader imageLoader;
@Override
public void onAttach(Activity host) {
super.onAttach(host);
ImageLoaderConfiguration ilConfig=
new ImageLoaderConfiguration.Builder(getActivity()).build();
imageLoader=ImageLoader.getInstance();
imageLoader.init(ilConfig);
}
@Override
public void onActivityCreated(Bundle state) {
super.onActivityCreated(state);
String[] from=
{ MediaStore.Video.Media.TITLE, MediaStore.Video.Media._ID };
int[] to= { android.R.id.text1, R.id.thumbnail };
SimpleCursorAdapter adapter=
new SimpleCursorAdapter(getActivity(), R.layout.row, null,
from, to, 0);
adapter.setViewBinder(this);
setListAdapter(adapter);
getLoaderManager().initLoader(0, null, this);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
CursorAdapter adapter=(CursorAdapter)getListAdapter();
Cursor c=(Cursor)adapter.getItem(position);
int uriColumn=c.getColumnIndex(MediaStore.Video.Media.DATA);
int mimeTypeColumn=
c.getColumnIndex(MediaStore.Video.Media.MIME_TYPE);
getContract().onVideoSelected(c.getString(uriColumn),
c.getString(mimeTypeColumn));
}
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
return(new CursorLoader(
getActivity(),
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
null, null, null,
MediaStore.Video.Media.TITLE));
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
((CursorAdapter)getListAdapter()).swapCursor(c);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
((CursorAdapter)getListAdapter()).swapCursor(null);
}
@Override
public boolean setViewValue(View v, Cursor c, int column) {
if (column == c.getColumnIndex(MediaStore.Video.Media._ID)) {
Uri video=
ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
c.getInt(column));
DisplayImageOptions opts=new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_media_video_poster)
.build();
imageLoader.displayImage(video.toString(), (ImageView)v, opts);
return(true);
}
return(false);
}
interface Contract {
void onVideoSelected(String uri, String mimeType);
}
}
并记录:
---
------ beginning of crash
02-17 09:29:27.230 14035-14035/ro.vrt.videoplayerstreaming E/AndroidRuntime: FATAL EXCEPTION: main
Process: ro.vrt.videoplayerstreaming, PID: 14035
android.view.InflateException: Binary XML file line #7: Binary XML file line #2: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at ro.vrt.videoplayerstreaming.ListVideoViewFragment.onCreateView(ListVideoViewFragment.java:21)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
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)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:941)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at ro.vrt.videoplayerstreaming.ListVideoViewFragment.onCreateView(ListVideoViewFragment.java:21)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
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)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f120124, tag null, or parent id 0xffffffff with another fragment for ro.vrt.videoplayerstreaming.VideosFragment
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2205)
at android.app.FragmentController.onCreateView(FragmentController.java:98)
at android.app.Activity.onCreateView(Activity.java:5546)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:941)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at ro.vrt.videoplayerstreaming.ListVideoViewFragment.onCreateView(ListVideoViewFragment.java:21)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
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)
02-17 09:29:29.559 14035-14035/ro.vrt.videoplayerstreaming I/Process: Sending signal. PID: 14035 SIG: 9
activity_list_video_view_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
layout="@layout/main"
android:layout_alignParentBottom="true"
android:layout_alignEnd="@+id/bShare"
android:layout_alignParentStart="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:layout_marginTop="70dp" />
</RelativeLayout>
和main.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/videos"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="ro.vrt.videoplayerstreaming.VideosFragment"
tools:layout="@layout/row" />
答案 0 :(得分:0)
我改变了listvideoviewfragment,你没有工作
public class ListVideoViewFragment extends Fragment {
private static View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.activity_list_video_view_fragment, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return view;
}
}