2015-07-31 12 views
13

Próbuję zaimplementować szufladę nawigacji, ale nadal pojawia się ten błąd. Widziałem podobne pytania, ale nie działało dla mnie. Mam następujący activity_main2.xml układ:Moduł DrawerLayout musi zostać zmierzony za pomocą błędu MeasureSpec.EXACTLY

<android.support.v4.widget.DrawerLayout 
    android:layout_width="match_parent" 
    android:id="@+id/drawerLayout" 
    android:layout_height="match_parent"> 

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 

    <RelativeLayout 
     android:layout_gravity="left|start" 
     android:layout_width="match_parent" 
     android:background="#fff" 
     android:layout_height="match_parent"> 

     <ListView 
      android:id="@+id/left_drawer" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:divider="#eee" 
      android:background="#fff" 
      android:dividerHeight="1dp" /> 
    </RelativeLayout> 
</android.support.v4.widget.DrawerLayout> 

Na moim activity_main2.java

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main2); 
    initView(); 
    if (toolbar != null) { 
     //toolbar.setTitle(""); 
     setSupportActionBar(toolbar); 
    } 
    initDrawer(); 
} 

private void initView() { 
    leftDrawerList = (ListView) findViewById(R.id.left_drawer); 
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 
    navigationDrawerAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, leftSliderData); 
    leftDrawerList.setAdapter(navigationDrawerAdapter); 
    leftDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      if(position == 1){ 
       drawerLayout.closeDrawer(Gravity.LEFT); 
      } 
     } 
    }); 
} 

private void initDrawer() { 
    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) { 

     @Override 
     public void onDrawerClosed(View drawerView) { 
      super.onDrawerClosed(drawerView); 
     } 

     @Override 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
     } 
    }; 
    drawerLayout.setDrawerListener(drawerToggle); 
} 

Mam nadzieję, że ktoś pomoże mi z właściwym kierunku.

Wielkie dzięki z góry!

+0

Skąd bierze się ten błąd? W środowisku wykonawczym lub w Android Studio? Czy korzystasz z najnowszej biblioteki pomocy technicznej? –

+0

przy użyciu Android Studio i wystąpił błąd środowiska wykonawczego: java.lang.IllegalArgumentException: DrawerLayout należy zmierzyć za pomocą metody MeasureSpec.EXACTLY. –

+0

Zobacz mój numer https: // stackoverflow.com/questions/32515527/illegal-argument-exception-drawerlayout-must-be-measured-with-measurespec-exac/47882575 # 47882575 –

Odpowiedz

23

My DrawerLayout był wewnątrz LinearLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:background="@drawable/abohawawallpapersqr" 
    tools:context=".MainActivity"> 

     <include layout="@layout/toolbar" /> 

     <android.support.v4.widget.DrawerLayout 
      android:id="@+id/drawerLayout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

     <!-- ...... --> 
     </android.support.v4.widget.DrawerLayout> 
</LinearLayout> 

Potem rozwiązał ten problem poprzez zmianę layout_height="wrap_content" do layout_height="match_parent" z LinearLayout .Tak Kod jest:

<LinearLayout 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" 
    android:orientation="vertical" 
    android:background="@drawable/abohawawallpapersqr" 
    tools:context=".MainActivity"> 
5

Upewnij się, że działalność ta nie jest czynność "Dialog", sprawdź w manifeście. Popełniłem ten błąd i dostałem błąd.

13

Dla tych, którzy jeszcze wyjątek, zapewniają wysokość twoi DrawerLayout „S jest match_parent

+0

dzięki za odpowiedź Twoje rozwiązanie zadziałało dla mnie –

0

użyciu Android.Support.V4.Widget; przy użyciu Androida.Util;

. . .

class MyDrawerLayout: DrawerLayout 
{ 
    public MyDrawerLayout(Context context) : base(context) 
    { 

    } 
    public MyDrawerLayout(Context context, IAttributeSet attrs) : base(context, attrs) 
    { 

    } 
    public MyDrawerLayout(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle) 
    { 

    } 
    public MyDrawerLayout(IntPtr context, Android .Runtime.JniHandleOwnership jniHandleOwnership) : base(context, jniHandleOwnership) 
    { 

    } 

    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec) 
    { 
     widthMeasureSpec = MeasureSpec.MakeMeasureSpec(MeasureSpec.GetSize(widthMeasureSpec), MeasureSpecMode.Exactly); 
     heightMeasureSpec = MeasureSpec.MakeMeasureSpec(MeasureSpec.GetSize(heightMeasureSpec), MeasureSpecMode.Exactly); 

     base.OnMeasure(widthMeasureSpec, heightMeasureSpec); 

    } 




} 
0

Przez pomyłkę, mam ustawić motyw dialogowe zwyczaj aktywności zamiast motywu działalności zwyczaj:

activity.setTheme(R.style.StyledDialogTheme); 

Po poprawiłem to, że workes ponownie.

-3

Got do produkcji->Clean projektu następnie Odbuduj projekt błąd będzie rozwiązać

+0

Naprawdę ???? na Stackoverflow? –

5

To również może się zdarzyć, jeśli ustawiono layout_width do wrap_content. W moim przypadku został on rozwiązany poprzez ustawienie szerokości na match_parent. Mam nadzieję, że to pomaga komuś.

0

został rozwiązany mój problem, tworząc aktywność jak poniżej;

@Override 
public void onCreate(Bundle savedInstanceState) { 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mainmenu); 
} 
5

Może się to również zdarzyć z powodu nowego ConstraintLayout. Upewnij się, że układ szuflady nie znajduje się w hierarchii ConstraintLayout.

To naprawdę głupi błąd ze strony Androida. Spróbuję złożyć na to błąd.

+0

Co wolisz zamiast tego? Zamieniam go na LinearLayout i otrzymuję: Błąd pompowania klasy android.support.constraint.LinearLayout – waseefakhtar

+1

Masz absolutną rację. RelativeLayout pracował dla mnie najlepiej tutaj. – waseefakhtar

Powiązane problemy