【问题标题】:Error FrameLayout in android 6 But not library [duplicate]android 6中的错误FrameLayout但不是库[重复]
【发布时间】:2017-06-05 13:23:52
【问题描述】:

Image for Example

子片段示例

OneFragment.java

public class OneFragment extends Fragment  {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("Fragment Demo", "Fragment_One onCreate()");
  }

  public OneFragment() {
    // Required empty public constructor
  }


  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_one, container, false);


    return rootView;
  }
  @Override
  public void onAttach(Context newBase) {
    super.onAttach(CalligraphyContextWrapper.wrap(newBase));
  }
}

fragment_one.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".fragment.OneFragment">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:layout_margin="16dp"
    android:orientation="vertical">

    <ImageView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tint="@color/grey_blue_100"
        android:src="@android:drawable/ic_dialog_alert"/>
    <TextView
        android:gravity="center"
        android:textSize="16sp"
        android:textColor="@color/grey_blue_100"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="نقشه گوگل در حال حاضر در دسترس نمی باشد"/>
</LinearLayout>

</RelativeLayout>

MainFragment.java

public class RegisterFragment extends Fragment implements View.OnClickListener {

  private FragmentActivity myContext;
  private Fragment fragment;
  private View rootView;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("Fragment Demo", "Fragment_One onCreate()");
  }

  public RegisterFragment() {
    // Required empty public constructor
  }


  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {

      rootView = inflater.inflate(R.layout.fragment_register_6, container, false);

    Button btnOne = (Button) rootView.findViewById(R.id.btn_one);
    btnOne.setOnClickListener(this);
    Button btnTwo = (Button) rootView.findViewById(R.id.btn_two);
    btnTwo.setOnClickListener(this);
    Button btnFinal = (Button) rootView.findViewById(R.id.btn_three);
    btnFinal.setOnClickListener(this);

    // This is default Fragment for RegisterFragment
      setFragmentUp(new OneFragment());

    return rootView;
  }

  @Override
  public void onClick(View v) {

    fragment = null;
    switch (v.getId()) {
      case R.id.btn_one:
        fragment = new OneFragment();
        break;
      case R.id.btn_two:
        fragment = new TwoFragment();
        break;
      case R.id.btn_three:
        fragment = new FinalFragment();
        break;
      default:
        break;
    }

    if (fragment != null) {
      setFragmentUp(fragment);
    }
  }

  @Override
  public void onAttach(Activity activity) {
    myContext = (FragmentActivity) activity;
    super.onAttach(activity);
  }

  protected void setFragmentUp(Fragment fragment) {
    myContext.getSupportFragmentManager().beginTransaction()
      .replace(R.id.fragRegisterUp, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
  }

  @Override
  public void onAttach(Context newBase) {
    super.onAttach(CalligraphyContextWrapper.wrap(newBase));
  }
}

fragment_main.xml

<LinearLayout
    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"
    android:background="@color/colorAccent"
    android:orientation="vertical"
    tools:context="com.fanavartech.android.ham_bar_v002.fragment.RegisterFragment">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:layout_marginTop="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginBottom="16dp"
    android:background="@android:color/white"
    android:orientation="horizontal">

    <com.andexert.library.RippleView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        app:rv_rippleDuration="114"
        app:rv_color="@color/colorPrimaryDark"
        rv_centered="true">
    <Button
        android:id="@+id/btn_one"
        android:layout_width="match_parent"
        android:text="اطلاعات بار"
        android:textSize="16sp"
        android:textStyle="bold"
        android:background="@null"
        android:layout_height="match_parent"/>
    </com.andexert.library.RippleView>

    <com.andexert.library.RippleView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        app:rv_rippleDuration="114"
        app:rv_color="@color/colorPrimaryDark"
        rv_centered="true">
    <Button
        android:id="@+id/btn_two"
        android:text="اطلاعات مسیر"
        android:textSize="16sp"
        android:textStyle="bold"
        android:background="@null"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    </com.andexert.library.RippleView>

    <com.andexert.library.RippleView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        app:rv_rippleDuration="114"
        app:rv_color="@color/colorPrimaryDark"
        rv_centered="true">
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/btn_three"
        android:text="پرداخت"
        android:textSize="16sp"
        android:textStyle="bold"
        android:background="@null"/>
    </com.andexert.library.RippleView>

</LinearLayout>

    <FrameLayout
        android:id="@+id/fragRegister"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_margin="16dp"/>



</LinearLayout>

MainActivity.java

public class FirstActivity extends AppCompatActivity
  implements NavigationView.OnNavigationItemSelectedListener {

  private Toolbar toolbar;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
      getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
    }

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.drawer_open , R.string.drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    Toast.makeText(FirstActivity.this, "ثبت بار", Toast.LENGTH_SHORT).show();
    toolbar.setTitle(R.string.nav_register);
    Fragment fragment = new RegisterFragment();
    getSupportFragmentManager().beginTransaction()
      .replace(R.id.container_body, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}

activity_main.xml

<android.support.v4.widget.DrawerLayout
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/container_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar"/>

        </LinearLayout>


        <android.support.percent.PercentFrameLayout
            android:id="@+id/container_body"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"/>


    </LinearLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/header"
        app:menu="@menu/activity_main_drawer"/>

</android.support.v4.widget.DrawerLayout>

错误按摩

06-05 13:16:55.049 4867-4867/com.fanavartech.android.ham_bar__v002 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.ham_v002, PID: 4867
java.lang.IllegalStateException: Underflow in restore - more restores than saves
at android.graphics.Canvas.native_restore(Native Method)
at android.graphics.Canvas.restore(Canvas.java:540)                                                                                         
at com.andexert.library.RippleView.draw(RippleView.java:166)
at android.view.View.updateDisplayListIfDirty(View.java:15174)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2615)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2434)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

对于 Android 版本 6 错误,但在较低的 Android 上正常工作。 问题出在哪里?

【问题讨论】:

    标签: java android android-fragments


    【解决方案1】:

    您可以尝试两种方法:

    • “只需将您的 -targetSdkVersion 降级到 22 它适用于所有 版本” - 如果您还没有启动您的应用程序,这很好用 targetSdkVersion 23。
    • 如果您已经使用 targetSdkVersion 23 启动了您的应用程序,那么, 而不是添加 compile 'com.github.traex.rippleeffect:library:1.3' 在依赖项中,通过单击链接下载项目 - https://codeload.github.com/traex/RippleEffect/zip/master

    从中获取库并在您的应用中使用它。它可能会起作用:)

    【讨论】:

    • 但是我的问题不是库,库是添加的。
    • 将 targetSdkVersion 降级到 22 就可以了
    • 它有效吗?
    • 是的,“targetSdkVersion 到 22 它适用于所有版本”
    猜你喜欢
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 2016-04-04
    • 2020-03-22
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多