Ich habe DrawerLayout in meiner App enthalten. Ich habe Toolbar und "Home Button" hinzugefügt. Das Sliding-Menü funktioniert einwandfrei, mit Ausnahme dieser "Menüschaltfläche" auf der linken Seite der Symbolleiste. Problem ist, wenn ich darauf klicke, schaltet sich meine App ohne Grund ab. Ich habe keine Ahnung, warum es schließt. Es sollte verstecktes gleitendes Menü öffnen. Wenn ich von links nach rechts wische, wird dieses Menü angezeigt, aber wenn die Schaltfläche angeklickt wird, wird die App sofort beendet.Toggle-Menü Schaltfläche nicht gleitende Menü öffnen und heruntergefahren App
Unten ist mein Menü XML-Code:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include layout="@layout/activity_main_sliding_content_main" />
<!--
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>-->
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
UND ENTHALTEN ANSICHT:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimaryDark"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:visibility="visible"/>
<ListView
android:id="@+id/listviewmain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_above="@+id/adView"
android:layout_below="@+id/my_toolbar" />
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/progressText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:text="Name"
android:layout_below="@+id/progressbar"
android:layout_centerHorizontal="true"
android:textAlignment="center"
/>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
JAVA Code unten:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
setContentView(R.layout.maincontent_slidingmenu);
mainRelativeLayout = (RelativeLayout) findViewById(R.id.mainRelativeLayout);
// mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
hiddenLayout = (RelativeLayout) findViewById(R.id.hiddenLayout);
myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
listView = (ListView) findViewById(R.id.listviewmain);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
progressView = (TextView) findViewById(R.id.progressText);
setSupportActionBar(myToolbar);
setupSlidingMenu(savedInstanceState);
setUpActionBarDrawerToggle();
setOnClickListners();
loadDataIntoMmemory();
setUpAdds();
}
private void setupSlidingMenu(Bundle savedInstanceState)
{
mTitle = mDrawerTitle = getTitle();
menuTitles = getResources().getStringArray(R.array.titles);
menuIcons = getResources().obtainTypedArray(R.array.icons);
mDrawerList = (ListView) findViewById(R.id.left_drawer) ;
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < menuTitles.length; i++) {
RowItem items = new RowItem(menuTitles[i], menuIcons.getResourceId(
i, -1));
rowItems.add(items);
}
menuIcons.recycle();
customSlidingAdapter = new CustomSlidingMenuAdapter(getApplicationContext(), rowItems);
System.out.println(customSlidingAdapter.isEmpty());
mDrawerList.setAdapter(customSlidingAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
/*mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());*/
};
private void setUpActionBarDrawerToggle()
{
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//new ActionBarDrawerToggle(myToolbar,mDrawerLayout, R.string.opened, R.string.zamkniecie);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.opened, R.string.zamkniecie) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
myToolbar.setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
syncState();
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
myToolbar.setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
syncState();
}
@Override
public void setToolbarNavigationClickListener(View.OnClickListener onToolbarNavigationClickListener) {
super.setToolbarNavigationClickListener(onToolbarNavigationClickListener);
mDrawerLayout.openDrawer(mDrawerList);
}
};
// Set the drawer toggle as the DrawerListener
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerLayout.addDrawerListener(mDrawerToggle);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER
return true;
case R.id.action_settings:
// User chose the "Settings" item, show the app settings UI...
return true;
case R.id.action_favorite:
// User chose the "Favorite" action, mark the current item
// as a favorite...
return true;
case R.id.action_searching:
/*// MenuItem searchItem = menu.findItem(R.id.action_searching);
//searchView = (SearchView) MenuItemCompat.getActionView();
searchView.setOnQueryTextListener(this);*/
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
//getActionBar().setTitle(mTitle);
myToolbar.setTitle(mTitle);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
//boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
//menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
mDrawerToggle.syncState();
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
// menu.findItem(R.id.left_drawer).setVisible(!drawerOpen);
// return super.onPrepareOptionsMenu(menu);
return super.onPrepareOptionsMenu(menu);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// Sync the toggle state after onRestoreInstanceState has occurred.
// mDrawerToggle.syncState();
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// Pass any configuration change to the drawer toggls
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_searching);
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
return true;
}
Blick auf dem Stack-Trace hinzufügen, um die Ursache des Absturzes zu ermitteln. http://stackoverflow.com/questions/23353173 Wenn Sie weitere Unterstützung benötigen, müssen Sie Ihre Frage bearbeiten, um diese Ablaufverfolgung und den relevanten Java-Code bereitzustellen. –
Problem ist, dass es ohne Informationen in Android-Monitor-Abschnitt geschlossen ist. Was soll ich in dieser Situation tun? – DKM
Post-Stack-Trace und die Java-Klasse mit diesem Layout. I –