问题
for the issue in question. Tried Searching for solutions and says getChildFragmentManager/getSupportFragmentManager should be used, but the problem is even with updated android sdk lib using the android support v13 (which has v4) installed, Eclipse Luna still cannot recognize getChildFragmentManager/getSupportFragmentManager. I also tried importing the support.app.v4 namespace but that will only make the ScreenSlidePagerAdapter's FragmentManager throw a type error.
DialogFragment Class which contains the view pager:
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
public class EnlargeItemPicture extends DialogFragment {
/**
* The number of pages to show in the view slider
*/
private static int NUM_PAGES = 1; // [Temp] set the total page number according to total items under current selected category instead next time
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager pager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter pagerAdapter;
private View customDialogView;
private String imagePath;
private String itemName;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// instead of inflating the activity with imageview only, inflate the viewpager activity here
customDialogView = inflater.inflate(R.layout.item_pager, null);
// Instantiate a ViewPager and a PagerAdapter.
pager = (ViewPager) customDialogView.findViewById(R.id.pager);
pagerAdapter = new ScreenSlidePagerAdapter(super.getFragmentManager());
pager.setAdapter(pagerAdapter);
/**
* Inflate and set the layout for the dialog
* Pass null as the parent view because its going in the dialog layout
* customDialogView = inflater.inflate(R.layout.dialog_item_picture, null);
* ImageView img = (ImageView) customDialogView.findViewById(R.id.enlargepicture);
* img.setImageBitmap(BitmapFactory.decodeFile(imagePath));
*/
builder.setView(customDialogView)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder.create();
}
public EnlargeItemPicture(String picturePath, String itemName)
{
imagePath = picturePath;
this.itemName = itemName;
}
/**
* A simple pager adapter that represents n objects, in
* sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return ScreenSlidePageItemViewFragment.create(position, "");
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
ScreenSlidePageItemViewFragment Class that holds the fragment to be inserted to viewpager:
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ScreenSlidePageItemViewFragment extends Fragment {
/**
* The argument key for the page number this fragment represents.
*/
public static final String ARG_PAGE = "page";
/**
* The argument key for the item Title this fragment represents.
*/
public static final String ARG_ITEM_TITLE = "item_title";
/**
* The fragment's page number, which is set to the argument value for {@link #ARG_PAGE}.
*/
private int pageNumber;
private String itemTitle;
/**
* Factory method for this fragment class. Constructs a new fragment for the given page number and Title.
*/
public static ScreenSlidePageItemViewFragment create(int pageNumber, String itemTitle) {
ScreenSlidePageItemViewFragment fragment = new ScreenSlidePageItemViewFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
args.putString(ARG_ITEM_TITLE, itemTitle);
fragment.setArguments(args);
return fragment;
}
/**
* Factory method for this fragment class. Constructs a new fragment.
*/
public ScreenSlidePageItemViewFragment()
{ }
/**
* Gets the arguments and pass to this properties
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageNumber = getArguments().getInt(ARG_PAGE);
itemTitle = getArguments().getString(ARG_ITEM_TITLE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout containing a title and body text.
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.item_page_view, container, true);
/// TODO: add listeners to each views here
return rootView;
}
/**
* Returns the page number represented by this fragment object.
*/
public int getPageNumber() {
return pageNumber;
}
/**
* Returns the item title represented by this fragment object.
*/
public String getItemTitle(){
return itemTitle;
}
}
Here is the XML file for layout with viewpager:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Here is the XML file for layout that will be inserted to viewpager:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/itemimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/itemDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/minusbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="-" />
<Button
android:id="@+id/addbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/minusbtn"
android:text="+" />
<TextView
android:id="@+id/itemTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/addbtn"
android:layout_alignBottom="@+id/addbtn"
android:layout_toLeftOf="@+id/addbtn"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</LinearLayout>
I'm at lost here, or perhaps i had it implemented wrong? or something that i had missed?
Plenty of Thanks in Advance!
回答1:
It may not be an answer in a way, at least it solve one of the causes which gives a new type of exception in which i will post another question if i cannot resolved it with answers from similar problems. The one that got resolved here is I am now able to use the getChildFragmentManager and getSupportFragmentManager function and the answer can be found here: Eclipse Android missing getSupportedFragmentManager and getChildFragmentManager.
回答2:
I think you should use:
View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
Instead of:
Dialog onCreateDialog(Bundle savedInstanceState);
It's helped for me.
Also, check out this link:
fragments in viewpager, no view found error
来源:https://stackoverflow.com/questions/31335482/android-viewpager-inside-dialogfragment-app-crashed-with-no-view-found-for-id-u