viewpager instantiateItem() 方法的错误,无法在片段中膨胀布局

viewpager instantiateItem() method's error and not able to inflate layout in fragment

错误:

主要活动代码为空,布局为:

<?xml version="1.0" encoding="UTF-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<fragment
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/fragment"
  android:name="com.example.circularview.CarFragment"
  tools:layout="@layout/fragment_car" />
</RelativeLayout>

片段(carFragment)代码:

     public class CarFragment extends Fragment {
      View rootView;
      ViewPager event_pager;
      private ArrayList<MyLocation> latLngsArrayList;

      @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_car, container, false);

        event_pager = rootView.findViewById(R.id.event_pager);
        latLngsArrayList = new ArrayList<>();
        latLngsArrayList = GlobalUtils.getLatLongArray();
        MapViewPagerAdapter adapter = new MapViewPagerAdapter(getActivity(), latLngsArrayList);
        event_pager.setAdapter(adapter);
        return rootView;

  }
}

和 CarFragment 布局代码:

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">

  <TextView
    android:id="@+id/mapView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintVertical_bias="1.0"/>


  <androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.77"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="507dp"/>

  <androidx.viewpager.widget.ViewPager
    android:id="@+id/event_pager"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="0dp"
    app:layout_constraintBottom_toBottomOf="@+id/mapView"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline">

  </androidx.viewpager.widget.ViewPager>
</androidx.constraintlayout.widget.ConstraintLayout>

我的适配器:

    public class MapViewPagerAdapter extends PagerAdapter {
  View   itemView;
  ArrayList<MyLocation> arr_LocationList;
  Context context;

  public MapViewPagerAdapter(Context context, ArrayList<MyLocation> arr_ExploreList) {
    this.context = context;
    this.arr_LocationList = arr_ExploreList;
  }

  @Override
  public int getCount() {
    return arr_LocationList.size();
  }

  @Override
  public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == arg1;
  }

  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
  }

  @NonNull
  @Override
  public View instantiateItem(ViewGroup container, int position) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    itemView = inflater.inflate(R.layout.recycler_mapview, null);

    container.addView(itemView);

    return itemView;
  }

}

适配器中出现错误:

  @NonNull
  @Override
  public View instantiateItem(ViewGroup container, int position) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    // error  error  error  error 
    itemView = inflater.inflate(R.layout.recycler_mapview, null);

    container.addView(itemView);

    return itemView;
  }

因为我认为它应该来自我的项目布局,这里是

ITEM 的布局:

 <?xml version="1.0"?>

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:paddingBottom="15dp"
  android:paddingTop="15dp">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_marginLeft="35dp"
    android:layout_marginRight="45dp"
    android:background="@drawable/mapview"
    android:orientation="horizontal"
    android:layout_marginStart="35dp"
    android:layout_marginEnd="45dp">

    <com.makeramen.roundedimageview.RoundedImageView
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight=".3"
      android:scaleType="fitXY"
      android:src="@mipmap/car"
      app:riv_corner_radius_bottom_left="20dp"
      app:riv_corner_radius_top_left="20dp"
      app:riv_mutate_background="true"
      app:srcCompat="@mipmap/car" />

    <LinearLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="0.63"
      android:orientation="vertical">

      <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center|left"
        android:text="Modern Era Car Servicing"
        android:textColor="#000"
        android:textSize="14dp"
        tools:ignore="RtlHardcoded" />

      <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="09388695027"
        android:layout_marginStart="10dp" />
    </LinearLayout>


  </LinearLayout>
</RelativeLayout>

logcat:

    05-26 15:32:43.007 12447-12447/com.example.circularview E/dalvikvm: Could not find class 'androidx.core.view.ViewCompat', referenced from method androidx.core.view.ViewCompat.addOnUnhandledKeyEventListener
05-26 15:32:43.007 12447-12447/com.example.circularview E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method androidx.core.view.ViewCompat.dispatchApplyWindowInsets
05-26 15:32:43.017 12447-12447/com.example.circularview E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method androidx.core.view.ViewCompat.onApplyWindowInsets
05-26 15:32:43.017 12447-12447/com.example.circularview E/dalvikvm: Could not find class 'android.view.View$OnUnhandledKeyEventListener', referenced from method androidx.core.view.ViewCompat.removeOnUnhandledKeyEventListener
05-26 15:32:43.017 12447-12447/com.example.circularview E/dalvikvm: Could not find class 'androidx.core.view.ViewCompat', referenced from method androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener
05-26 15:32:43.057 12447-12447/com.example.circularview E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method androidx.appcompat.widget.AppCompatImageHelper.hasOverlappingRendering
05-26 15:32:43.087 12447-12447/com.example.circularview E/dalvikvm: Could not find class 'android.view.textclassifier.TextClassificationManager', referenced from method androidx.appcompat.widget.AppCompatTextClassifierHelper.getTextClassifier
05-26 15:32:43.127 12447-12447/com.example.circularview E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.circularview, PID: 12447
    android.view.InflateException: Binary XML file line #12: Error inflating class <unknown>
        at android.view.LayoutInflater.createView(LayoutInflater.java:620)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
        at com.example.circularview.MapViewPagerAdapter.instantiateItem(MapViewPagerAdapter.java:43)
        at com.example.circularview.MapViewPagerAdapter.instantiateItem(MapViewPagerAdapter.java:13)
        at androidx.viewpager.widget.ViewPager.addNewItem(ViewPager.java:1010)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1158)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
        at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
        at android.view.View.measure(View.java:16497)
        at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1676)
        at android.view.View.measure(View.java:16497)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:403)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
        at android.view.View.measure(View.java:16497)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1940)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1137)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1319)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1024)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5694)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:544)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5019)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflec

所有这些代码都是我之前制作的另一个应用程序的一部分......它在那里工作但不在这里......因为在另一个应用程序中它与地图和用户位置标记一起工作并且有一些属性,如向右滑动或向左滑动我在这里没有使用过的。 我的代码有什么问题? 它适用于 MAINACTIVITY 但不适用于 FRAGMENT

最后我发现了我的问题 is.in RelativeLayout 下的 ITEM 布局是我设置背景的线性布局:android:background="@drawable/mapview" 当我清理它时它工作 perfectly.this drawable 是一个圆角视图。

    <?xml version="1.0" encoding="utf-8"?>
<shape
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
  <corners
    android:radius="20dp"/>
  <gradient
    android:startColor="#ffffffff"
    android:centerColor="#ffffffff"
    android:endColor="#ffffffff"
    />
</shape>

它的名字是 (drawable-v24),我认为我的问题与 (V24)

有关