可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to implement a Navigation drawer, but I keep getting this error. I saw the similar questions but did not work for me. I have the following layout activity_main2.xml:
On my 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( 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); }
Hopefully someone will help me with the right direction.
Many thanks in advance!
回答1:
My DrawerLayout was inside a LinearLayout:
Then I solved the problem by changing the layout_height="wrap_content"
to layout_height="match_parent"
of LinearLayout
.So the code is:
回答2:
For those who still have an exception, ensure your DrawerLayout
's height is match_parent
回答3:
Make sure your activity is not a "Dialog" activity, check in the manifest. I made this mistake and got the error.
回答4:
This can also happen if you have set layout_width
to wrap_content
. In my case it was solved by setting width to match_parent
. Hope this helps someone.
回答5:
This can also happen because of the new ConstraintLayout
. Make sure the drawer layout isn't in the hierarchy of ConstraintLayout
.
This is a really silly bug on Android's part. I will try to file a bug for it.
回答6:
using Android.Support.V4.Widget; using Android.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); } }
回答7:
By mistake, I set a custom dialog theme to the activity instead of a custom activity theme:
activity.setTheme(R.style.StyledDialogTheme);
After i corrected this, it workes again.
回答8:
requestWindowFeature(Window.FEATURE_NO_TITLE);
was solved my problem by creating the activity like below;
@Override public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.mainmenu); }
回答9:
Got to Build->Clean project then Rebuild project error will be solve