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

五迷三道 提交于 2020-05-30 08:12:08

问题


error:

and mainactivity code is empty and layout is:

<?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>

fragment (carFragment) code:

     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;

  }
}

and CarFragment layout code:

 <?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>

my Adapter:

    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;
  }

}

error is here in adapter:

  @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;
  }

as i think it should be from my items layout ,here it is

ITEM's Layout:

 <?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$2', 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$1', 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

all of these codes are part of another app that i made before...it works there but not here...because in the other app it work with map and users location marker and have some atrributes like slide right or slide left that i didnt used here . what is my codes problem? it works in MAINACTIVITY but not in FRAGMENT


回答1:


finally i found out where my problem is.in ITEM's LAYOUT under RelativeLayout is a Linear Layout that i set background : android:background="@drawable/mapview" and when i clean it it work perfectly.this drawable is a rounded corners view.

    <?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>

that its name is (drawable-v24) and i think my problem was related to (V24)



来源:https://stackoverflow.com/questions/62024952/viewpager-instantiateitem-methods-error-and-not-able-to-inflate-layout-in-fra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!