0

解决相关问题。尝试搜索解决方案,并说getChildFragmentManager/getSupportFragmentManager应该使用,但问题是即使更新android sdk库使用Android支持v13(已v4)安装,Eclipse Luna仍然无法识别getChildFragmentManager/getSupportFragmentManager。我也尝试导入support.app.v4命名空间,但这只会让ScreenSlidePagerAdapter的FragmentManager引发一个类型错误。对话框片段中的Android viewpager,应用程序崩溃,没有找到id的视图,使用API​​ 17

DialogFragment类,其中包含的视图寻呼机:

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:

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文件:

<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文件将inser Ted 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> 

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

大量提前致谢!

+0

嗨能够通过仔细替换每个使用支持LIB访问getSupportFragmentManager功能,但仍得到发现ID ??无视图。我仍然在寻找如何访问getChildFragmentManager函数的方法,看看它是否有帮助。 –

+0

阅读答案,解决未定义的功能问题。在使用getChildFragmentManager而不是getFragmentManager函数后,会出现一个新的错误,即Fragment没有视图。我目前正在研究导致异常的原因。 –

回答

0

我认为你应该使用:

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

相反的:

Dialog onCreateDialog(Bundle savedInstanceState); 

它帮助我。

此外,检查出此链接:

fragments in viewpager, no view found error

相关问题