里面dialogfragment的Android viewpager,应用程序崩溃,没有发现使用API 17号视图 [英] Android viewpager inside dialogfragment, app crashed with no view found for id using API 17
问题描述
所讨论的问题。试图寻找解决办法,并说getChildFragmentManager / getSupportFragmentManager应该使用,但问题是即使有更新的Android SDK中的lib使用Android的支持V13(其中有4版)安装时,Eclipse月神仍然无法识别getChildFragmentManager / getSupportFragmentManager。我也尝试导入support.app.v4命名空间,但只会让ScreenSlidePagerAdapter的FragmentManager抛出一个类型的错误。
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类,它包含的观点寻呼机:
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类保存的片段被插入到viewpager:
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;
}
}
下面是与viewpager布局XML文件:
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" />
下面是将被插入到viewpager布局XML文件:
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?
在此先感谢充沛!
推荐答案
这可能不是在某种程度上答案,至少它解决它提供了一种新类型的异常,其中我会后,如果另一个问题的原因之一我不能用类似的问题的答案解决它。那到这里解决的是我现在能够使用getChildFragmentManager和getSupportFragmentManager功能,答案可以在这里找到:<一href=\"http://stackoverflow.com/questions/31355701/eclipse-android-missing-getsupportedfragmentmanager-and-getchildfragmentmanager\">Eclipse Android的缺失getSupportedFragmentManager和getChildFragmentManager 。
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.
这篇关于里面dialogfragment的Android viewpager,应用程序崩溃,没有发现使用API 17号视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!