Android dialogfragment 中的 viewpager,应用程序崩溃,使用 API 17 找不到 id 的视图

Android viewpager inside dialogfragment, app crashed with no view found for id using API 17

针对相关问题。尝试搜索解决方案并说应该使用 getChildFragmentManager/getSupportFragmentManager,但问题是即使更新了 android sdk lib 使用 android 支持 v13(已安装 v4),Eclipse Luna 仍然无法识别getChildFragmentManager/getSupportFragmentManager。我还尝试导入 support.app.v4 命名空间,但这只会使 ScreenSlidePagerAdapter 的 FragmentManager 抛出类型错误。

包含视图寻呼机的 DialogFragment Class:

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 包含要插入到 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;
}
}

这里是 XML 用于使用 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" />

这里是 XML 将插入到 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>

我在这里迷路了,或者我实施错了?或者我错过了什么?

非常感谢!

它在某种程度上可能不是一个答案,至少它解决了一个导致新型异常的原因,如果我不能用类似的答案解决它,我将 post 另一个问题问题。这里解决的问题是我现在可以使用 getChildFragmentManager 和 getSupportFragmentManager 函数,答案可以在这里找到:.

我认为你应该使用:

View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

而不是:

Dialog onCreateDialog(Bundle savedInstanceState);

这对我很有帮助。

另外,看看这个 link: