android:DrawerLayout + PagerView = stackOverflow.Why?

时间:2017-02-17 16:35:34

标签: android drawerlayout

我希望我的应用程序具有多个状态的侧面菜单,并且它们之间有平滑过渡。为了做到这一点,我将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:致命异常:主要                                                                             流程: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)
....

1 个答案:

答案 0 :(得分:0)

问题在于我如何给片段视图充气 而不是

<!doctype html>
<html><head>
<meta charset="UTF-8">
	<title>Multiple Map Points</title> 
 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps Multiple Markers</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 
<body>
  <div id="map" style="width: 800px; height: 800px;" position="relative;"></div>

  <script type="text/javascript">
    var locations = [
      ['EMS', 32.575258, -117.061613, 4],
      ['FIRE', 32.958337, -117.096112, 3],
      ['HAZMAT', 32.728588, -117.100064, 2],
	    ['MVA', 32.556325, -117.055856, 1]
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(32.7157, -117.1611),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
  </script>


</body>
</html>

应该是

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

我认为它不会将我的布局添加到视图层次结构中,但是由于布局是空的&gt;没有可见的线索它在那里