gpt4 book ai didi

java.lang.IllegalArgumentException : No drawer view found with gravity LEFT in Navigation Drawer

转载 作者:行者123 更新时间:2023-12-01 18:08:59 34 4
gpt4 key购买 nike

我正在创建一个具有抽屉导航的应用程序。首先它正在工作,但是当我编辑工具栏的layout_gravity以结束时,我收到错误

java.lang.IllegalArgumentException: No drawer view found with gravity LEFT

MainActivity

我不知道我的代码出了什么问题,但我只更改了layout_gravity来结束。

    public class MainActivity extends AppCompatActivity {

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private CharSequence mTitle;
private CharSequence mDrawerTitle;
private ActionBarDrawerToggle mDrawerToggle;
private Toolbar topToolBar;

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

/****************************************************************************************
* DRAWER
****************************************************************************************/
mTitle = mDrawerTitle = " ";

topToolBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(topToolBar);
topToolBar.setLogo(R.drawable.logo);
topToolBar.setLogoDescription(" ");

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);

List<DrawerItem> drawerItems = new ArrayList<DrawerItem>();
drawerItems.add(new DrawerItem("Home"));
drawerItems.add(new DrawerItem("Student"));
drawerItems.add(new DrawerItem("Teacher"));

mDrawerList.setAdapter(new DrawerAdapter(this, drawerItems));
mDrawerToggle = new ActionBarDrawerToggle(MainActivity.this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

/**
* Called when a drawer has settled in a completely closed state.
*/
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}

/**
* Called when a drawer has settled in a completely open state.
*/
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};

// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.setDrawerIndicatorEnabled(true);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

selectItem(position);
}
});

}

private void selectItem(int position){

switch(position) {
default:
case 0:
break;
case 1:
Intent intent2 = new Intent(MainActivity.this, StudentActivity.class);
startActivity(intent2);
finish();
break;
case 2:
Intent intent3 = new Intent(MainActivity.this, TeacherActivity.class);
startActivity(intent3);
finish();
break;
}

mDrawerList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

xml

<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="#ffffff">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="end"
android:background="#ffffff"
android:minHeight="?attr/actionBarSize" />

<android.support.v4.widget.DrawerLayout
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"
android:background="#ffffff">


</LinearLayout>

<!-- NAVIGATION DRAWER -->
<ListView android:id="@+id/left_drawer"
android:layout_width="160dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="#b1b1b1"
android:dividerHeight="0.5dp"
android:background="#ffffff"
/>

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

</LinearLayout>

logcat error

FATAL EXCEPTION: main
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime: Process: com.example.jathniel.drawertesting, PID: 13881
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime: java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime: at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1464)
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime: at android.support.v7.app.ActionBarDrawerToggle.toggle(ActionBarDrawerToggle.java:288)
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime: at android.support.v7.app.ActionBarDrawerToggle.onOptionsItemSelected(ActionBarDrawerToggle.java:278)
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime: at com.example.jathniel.drawertesting.MainActivity.onOptionsItemSelected(MainActivity.java:141)
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime: at android.app.Activity.onMenuItemSelected(Activity.java:3024)
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime: at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)

最佳答案

在您的 DrawerLayout 中添加 android:layout_gravity="left",

关于java.lang.IllegalArgumentException : No drawer view found with gravity LEFT in Navigation Drawer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34326183/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com