2016-11-25 6 views
0

Ich bin neu in der Entwicklung von Android-Apps und versuche, eine App zu erstellen. Ich habe meine Navigationsschublade erfolgreich erstellt, aber Ich möchte, dass die Menüelemente beim Klicken auf eine andere Aktivität geöffnet werden. Ich habe verschiedene Antworten auf SO versucht, aber meine App scheint jedes Mal abzustürzen, wenn ich meine App nach der Anwendung einer der Antworten ausgeführt habe. Bitte, was könnte der Grund dafür sein.Menüeintrag "Schublade erstellen" Neue Aktivität öffnen, wenn Sie darauf klicken

menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

<item 
    android:id="@+id/my_account" 
    android:title="My Account" 
    android:icon="@mipmap/ic_person_white_24dp" 
    android:onClick="settings"></item> 

<item 
    android:id="@+id/my_settings" 
    android:title="Settings"></item> 

<item 
    android:id="@+id/Logout" 
    android:title="Log Out" 
    android:icon="@mipmap/ic_exit_to_app_white_24dp"></item> 

</menu> 

activity.java

package com.example.orume.export; 

import android.content.Intent; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 


public class WorkActivity extends AppCompatActivity { 

private DrawerLayout mDrawerLayout; 
private ActionBarDrawerToggle mToggle; 

private Toolbar mToolbar; 

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

    mToolbar = (Toolbar)findViewById(R.id.toolbar); 
    setSupportActionBar(mToolbar); 

    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout); 
    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.string.open, 
R.string.close); 

    mDrawerLayout.addDrawerListener(mToggle); 
    mToggle.syncState(); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    final Button bExplore = (Button) findViewById(R.id.bExplore); 
    bExplore.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View view) { 
      Intent exploreIntent = new Intent(WorkActivity.this, 
RegisterActivity.class); 
      WorkActivity.this.startActivity(exploreIntent); 
     } 
    }); 
} 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    if (mToggle.onOptionsItemSelected(item)){ 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 

} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu_search, menu); 

    return true; 
} 

} 

activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout  
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" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
android:id="@+id/drawerLayout" 
android:background="#d8dadc"> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <include layout="@layout/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 


<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dp"> 

</RelativeLayout> 

<android.support.design.widget.NavigationView 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    app:menu="@menu/navigation_menu" 
    android:layout_gravity="start" 
    android:background="@color/colorPrimary" 
    app:headerLayout="@layout/navigation_header"> 

</android.support.design.widget.NavigationView> 



<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginTop="60dp"> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:background="#d8dadc"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_alignParentBottom="false" 
      android:padding="10dp"> 

      <Button 
       android:layout_width="0dp" 
       android:layout_height="70dp" 
       android:layout_weight="0.5" 
       android:background="@mipmap/bg_tool" 
       android:id="@+id/bExplore" 
       android:layout_marginRight="5dp"/> 
      <Button 
       android:layout_width="0dp" 
       android:layout_height="70dp" 
       android:layout_weight="0.5" 
       android:background="@mipmap/bg_tool_2" 
       android:id="@+id/bPrice" 
       android:layout_marginLeft="5dp" 
       android:text="Explore" 
       android:textAllCaps="false" 
       android:textColor="#191919"/> 
     </LinearLayout> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_alignParentBottom="false" 
      android:padding="10dp" 
      android:layout_marginTop="75dp"> 

      <Button 
       android:layout_width="0dp" 
       android:layout_height="70dp" 
       android:layout_weight="0.5" 
       android:background="@mipmap/bg_tool" 
       android:id="@+id/bS" 
       android:layout_marginRight="5dp"/> 
      <Button 
       android:layout_width="0dp" 
       android:layout_height="70dp" 
       android:layout_weight="0.5" 
       android:background="@mipmap/bg_tool_2" 
       android:id="@+id/bA" 
       android:layout_marginLeft="5dp" 
       android:text="Explore" 
       android:textAllCaps="false" 
       android:textColor="#191919"/> 
     </LinearLayout> 
    </RelativeLayout> 
</ScrollView> 
</android.support.v4.widget.DrawerLayout> 
+0

möglich Duplikat hinzufügen http://stackoverflow.com/q/23626569/3758972 –

Antwort

0

In Ihrem menu.xml haben Sie diese Zeile android:onClick="settings" hinzugefügt. diese Zeile entfernen oder den folgenden Code in Ihrem MainActivity.java

public boolean settings(MenuItem item) { 
// actions 
return true; 
} 
Verwandte Themen