【问题标题】:android: DrawerLayout+PagerView=stackOverflow.Why?android: DrawerLayout+PagerView=stackOverflow。为什么?
【发布时间】:2017-02-17 16:35:34
【问题描述】:

我希望我的应用程序有带有多个状态的侧边菜单,它们之间可以平滑过渡。为了做到这一点,我将 PagerView 实例设置为导航抽屉。我的活动 xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.rhyboo.com.drawer_test.MainActivity">

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

<net.rhyboo.com.drawer_test.PuzzlePagerView
    android:layout_gravity="start"
    android:layout_height="match_parent"
    android:layout_width="240dp"
    android:background="@android:color/white"
    android:id="@+id/pager">
</net.rhyboo.com.drawer_test.PuzzlePagerView>

它适用于空的PagerView。但是,如果我为PagerView设置适配器,我得到堆栈溢出异常。这是我的活动类:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DrawerLayout drawer=(DrawerLayout) findViewById(R.id.drawer);
    drawer.setScrimColor(getResources().getColor(R.color.gameMenuDim));
    PuzzlePagerView pager=(PuzzlePagerView)findViewById(R.id.pager);
    GameMenuPagerAdapter menuAdapter=new GameMenuPagerAdapter(getSupportFragmentManager());
    pager.setAdapter(menuAdapter);
}
}

适配器:

public class GameMenuPagerAdapter extends FragmentPagerAdapter {
private GameMenuFragment gameMenu;
public GameMenuPagerAdapter(FragmentManager fm) {
    super(fm);
    gameMenu=new GameMenuFragment();
}
@Override
public Fragment getItem(int position) {
    Log.d("pager",position+"");
    switch (position) {
        case 0:
            return gameMenu;
    }
    return null;
}

@Override
public int getCount() {
    return 1;
}
}

ViewPager:

package net.rhyboo.com.drawer_test;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class PuzzlePagerView extends ViewPager {
private boolean touchEnabled;
public PuzzlePagerView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (this.touchEnabled) {
        return super.onTouchEvent(event);
    }

    return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (this.touchEnabled) {
        return super.onInterceptTouchEvent(event);
    }

    return false;
}

public void setPagingEnabled(boolean enabled) {
    this.touchEnabled = enabled;
}
}

片段:

public class GameMenuFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.game_menu,container);
}
}

堆栈跟踪:

E/AndroidRuntime: 致命异常: main 进程:net.rhyboo.com.drawer_test,PID:14588 java.lang.StackOverflowError: 堆栈大小 8MB
在 android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6274) 在 android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6278) 在 android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6278) 在 android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6278)
....

【问题讨论】:

  • 你能提供 PuzzlePagerView / GameMenuFragment 吗?
  • Stackoverflow error的可能重复
  • 乍一看,我猜你的问题在GameMenuFragment 内部,就像@Submersed 建议的那样,你能发布这些类的代码吗?
  • 在初始帖子中添加了 pagerView 和片段类
  • 如果我将片段视图膨胀中的容器参数更改为null,错误就会消失,但在这种情况下,它不会将我的布局添加到视图层次结构中。

标签: android drawerlayout


【解决方案1】:

问题是我如何膨胀片段视图 而不是

return inflater.inflate(R.layout.game_menu,container);

应该是

return inflater.inflate(R.layout.game_menu,container,false);

我认为它不会将我的布局添加到视图层次结构中,但这是由于布局是空的=>没有可见的线索

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-08
    • 2013-03-22
    • 1970-01-01
    • 2023-01-19
    • 2015-09-19
    • 2017-04-26
    • 1970-01-01
    • 2018-03-17
    相关资源
    最近更新 更多