0

Ich versuche, die Zurück-Schaltfläche in dieser wirklich einfachen Aktivität (die durch eine andere Aktivität instanziiert) zu aktivieren, aber ich bekomme immer die Nullzeiger Ausnahme und daher stürzt meine App ab, wenn ich Starten Sie die Aktivität (ohne die Zurück-Schaltfläche in der Aktionsleiste funktioniert die Aktivität großartig). Ich habe versucht, viele andere Lösung im Internet veröffentlicht, aber keiner arbeitete ... Ich folge dieser Anleitung: Android Developer providing up navigationZurück-Schaltfläche in der Aktionsleiste funktioniert nicht

Hier ist die Manifest-Datei:

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

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/Theme.AppCompat.Light.NoActionBar" 
     android:screenOrientation="portrait"> 
     <!--attività principale--> 
     <activity 
      android:name=".MainActivity" 
      android:launchMode="singleInstance"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <!--attività infixToPostfix--> 
     <activity 
      android:name=".InfixToPostfixActivity" 
      android:launchMode="singleTask" 
      android:label="@string/infix_to_postfix"/> 
     <!--attività figlia di infixToPostfix--> 
     <activity 
      android:name=".ShowPostfixProcedureActivity" 
      android:launchMode="standard" 
      android:label="@string/postfix_procedure" 
      android:parentActivityName=".InfixToPostfixActivity" /> 
    </application> 

</manifest> 

Hier ist die Java-Datei der Aktivität (ShowPostfixProcedureActivity):

package com.example.android.projectcalculator; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.NavUtils; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.text.Html; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 

import java.util.Stack; 

import static java.lang.Character.isDigit; 

public class ShowPostfixProcedureActivity extends AppCompatActivity { 

    private static String equation; 
    private static int openBracket; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.show_infix_to_postfix_jurney); 
     Toolbar myToolbar = (Toolbar) findViewById(R.id.infix_to_postfix_toolbar_procedure); 
     setSupportActionBar(myToolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     TextView txview = (TextView)findViewById(R.id.postfix_jurney_text_wall); 
     Stack<String> s = new Stack<String>(); 
     s = DataProcessor.ReturnProcess(); 
     txview.setText(s.pop()); 
     while (!s.empty()) 
     { 
      if (s.peek().charAt(0) == '<') 
      { 
       txview.append(Html.fromHtml(s.pop())); 
      } 
      else 
      { 
       txview.append(s.pop()); 
      } 
     } 
     //Log.d("SPPA/onCreate","text = "+DataProcessor.ReturnProcess()); 
    } 

    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.action_bar_postfix_procedure, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     switch (item.getItemId()) { 
      // Respond to the action bar's Up/Home button 
      case android.R.id.home: 
       NavUtils.navigateUpFromSameTask(this); 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 


} 

die XML-Datei in das Layout bezogen (show_infix_to_postfix_jurney):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <!--ActionBar in alto--> 
    <android.support.v7.widget.Toolbar 
     android:id="@+id/infix_to_postfix_toolbar_procedure" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="#42f480" 
     android:elevation="4dp" 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="vertical"> 

     <TextView 
      android:id="@+id/postfix_jurney_text_wall" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:padding="10sp" 
      android:textSize="20sp" 
      android:gravity="center" 
      android:background="#f2f2f2"/> 

    </ScrollView> 

</LinearLayout> 

Und schließlich die XML-Datei in der Navigationsleiste im Zusammenhang (action_bar_postfix_procedure):

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



</menu> 

Hier ist die Fehlermeldung:

10-29 17:14:43.841 18822-18822/com.example.android.projectcalculator E/AndroidRuntime: FATAL EXCEPTION: main 
                         Process: com.example.android.projectcalculator, PID: 18822 
                         Theme: themes:{} 
                         java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.MenuItem.setEnabled(boolean)' on a null object reference 
                          at com.example.android.projectcalculator.ShowPostfixProcedureActivity.onCreateOptionsMenu(ShowPostfixProcedureActivity.java:51) 
                          at android.app.Activity.onCreatePanelMenu(Activity.java:2852) 
                          at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:340) 
                          at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85) 
                          at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258) 
                          at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85) 
                          at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454) 
                          at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61) 
                          at android.os.Handler.handleCallback(Handler.java:739) 
                          at android.os.Handler.dispatchMessage(Handler.java:95) 
                          at android.os.Looper.loop(Looper.java:148) 
                          at android.app.ActivityThread.main(ActivityThread.java:5461) 
                          at java.lang.reflect.Method.invoke(Native Method) 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
+2

Das Menü XML ist leer schließen wollen ... Haben Sie wirklich brauchen die 'onCreateOptionsMenu' Methode? –

+2

Sie haben MENU xml leer gelassen, deshalb tritt die Null-Ponter-Ausnahme auf. –

+0

Es scheint auch, dass Ihr Stacktrace und Quellcode nicht korrelieren. Vielleicht versuchen Sie, Ihr Projekt zu säubern und sofort zu stoppen. Außerdem sollten Sie 'super.onCreateOptionsMenu (menu)' aufrufen. – tynn

Antwort

0

Ich weiß nicht, was die Linie NavUtils.navigateUpFromSameTask(this); tun soll . Wenn Sie die Aktivität auf dem Drücken der Zurück-Taste ersetzen Sie es mit dem Methodenaufruf onBackPressed()

@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 
    switch (item.getItemId()) { 
     // Respond to the action bar's Up/Home button 
     case android.R.id.home: 
      onBackPressed() 
      return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
+1

* Um nach oben zu navigieren, wenn die Wenn der Benutzer das App-Symbol drückt, können Sie die statische Methode der NavUtils-Klasse, navigateUpFromSameTask(), verwenden. Wenn Sie diese Methode aufrufen, wird die aktuelle Aktivität beendet und die entsprechende übergeordnete Aktivität gestartet (oder fortgesetzt). * - https://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp –

+0

@ cricket_007 thanks. Ich war mir nicht sicher, ob das eine eigene Klasse war, die er benutzte. Ich habe immer nur auf Rücken gedrückt, um Aktivitäten zu schließen, da ich die Absicht einstellen kann, wenn ich muss. –

Verwandte Themen