2016-07-30 7 views
1

Hallo Leute haben einige Probleme mit Code. Ich brauche eine neue Aktivität zu öffnen, wenn das Element unter dem Überlauf-Symbol in der Aktionsleiste geklickt wirdWie Sie andere Aktivität aufrufen, indem Sie auf ein Element unter Überlauf-Symbol in der Aktionsleiste klicken

Unten habe ich meinen Code

Ich möchte wissen, welchen Platz ich in meinem Codierung falsch mache und was kann die Lösung für sie

Vielen Dank im Voraus

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

    <item 
     android:id="@id/item_option1" 
     android:title="option1" 
     android:onclick="gotocontact"> 
     </item> 
    </menu> 

// Main xml file 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    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:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.example.mohammadzakriya.actionbar2.MainActivity"> 

</RelativeLayout> 

MainActivity.java

package com.example.mohammadzakriya.actionbar2; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 

public class MainActivity extends AppCompatActivity { 

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

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater menuInflater = getMenuInflater(); 
     menuInflater.inflate(R.menu.menu_main,menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch(item.getItemId()){ 
      case R.id.item_option1: 
       public void gotocontact(View view) 
      { 
       Intent intent = new Intent(this, Contact.class); 
       startActivity(intent); 
      } 
       break; 

      default: 
       break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

} 

Android-Manifest-Datei

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.mohammadzakriya.actionbar2"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".Contact"> 

     </activity> 
    </application> 

</manifest> 

Antwort

0

Folgendes sollte in Ordnung sein:

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch(item.getItemId()){ 
      case R.id.item_option1: 
       Intent intent = new Intent(this, Contact.class); 
       startActivity(intent); 
       break; 

      default: 
       break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
+0

Dank funktionierte es –

+0

kühlen. Sie können die Frage schließen, indem Sie diese als akzeptierte Antwort auswählen. – Shaishav

+0

ja, aber ich musste auch Änderungen in meinem Android-Menü XML-Datei vornehmen. musste den teil von android löschen: onclick = "gotocontact" ... ruhen auf die editierung in den angegebenen suggestions der code funktionierte gut –

0

Problem mit diesem Code ist ....

public void gotocontact(View view) 
     { 
      Intent intent = new Intent(this, Contact.class); 
      startActivity(intent); 
     } 

Sie eine Methode definieren aber nicht nennen es .... so wird die Methode nicht ausgeführt .... Sie müssen defi ne es in Ihrer Tätigkeit und von onOptionItemSelected Methode wie folgt nennen ....

package com.example.mohammadzakriya.actionbar2; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 

public class MainActivity extends AppCompatActivity { 

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

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater menuInflater = getMenuInflater(); 
     menuInflater.inflate(R.menu.menu_main,menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch(item.getItemId()){ 
      case R.id.item_option1: 
       gotocontact(); 
       break; 

      default: 
       break; 
     } 
     return super.onOptionsItemSelected(item); 
    } 



      public void gotocontact() 
       { 
        Intent intent = new Intent(this, Contact.class); 
        startActivity(intent); 
       } 
} 
Verwandte Themen