2016-05-22 20 views
0

Ich bin neu auf Android und dieses Problem für während haben: Kein Artikel auf der Aktionsleiste angezeigt, wie Sie im Bild unten sehen können:Android ActionBar zeigt keinen Artikel

enter image description here

Hier ist meine menu_main .xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="com.myappshop.myapp.MainActivity"> 

    <item 
     android:id="@+id/action_favorite" 
     android:icon="@drawable/share_icon" 
     android:title="@string/share" 
     app:showAsAction="always"/> 


    <item 
     android:id="@+id/action_settings" 
     android:orderInCategory="100" 
     android:title="@string/action_settings" 
     app:showAsAction="ifRoom" /> 

</menu> 

Und die MainActivity.java:

public class MainActivity extends AppCompatActivity { 
    private static final String TAG = MainActivity.class.getSimpleName(); 

    EditText mEditTextWord; 
    EditText mEditTextDefinition; 
    DictionaryDatabase mDB; 
    ListView mListView; 

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

     mDB = new DictionaryDatabase(this); 
     mEditTextWord = (EditText)findViewById(R.id.editTextWord); 
     mEditTextDefinition = (EditText)findViewById(R.id.editTextDefinition); 
     Button buttonAddUpdate = (Button)findViewById(R.id.buttonAddUpdate); 

     buttonAddUpdate.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         saveRecord(); 
        } 
       }); 



     mListView = (ListView)findViewById(R.id.listView); 
     mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View 
        view, int position, long id) { 
       Log.d(TAG, "the id is logged " + Long.toString(id)); 

       String nextId = String.valueOf(id+1); 
       Intent intent = new Intent(view.getContext(),DetailActivity.class); 
       intent.putExtra("key" ,mDB.getWord(id)+""); 
       intent.putExtra("value",mDB.getDefinition(id)+""); 
       intent.putExtra("nextId",nextId+""); 
       startActivity(intent); 
      } 
     }); 

     mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(MainActivity.this, 
         "Records deleted = " + mDB.deleteRecord(id), 
         Toast.LENGTH_SHORT).show(); 
       updateWordList(); 
       return true; 
      } 
     }); 
     updateWordList(); 

    } 


    private void saveRecord() { 
     mDB.saveRecord(mEditTextWord.getText().toString(), 
       mEditTextDefinition.getText().toString()); 
     mEditTextWord.setText(""); 
     mEditTextDefinition.setText(""); 
     updateWordList(); 
    } 

    private void updateWordList() { 
     SimpleCursorAdapter simpleCursorAdapter = new 
       SimpleCursorAdapter(this, 
       android.R.layout.simple_list_item_1, 
       mDB.getWordList(), 
       new String[]{"word"}, 
       new int[]{android.R.id.text1}, 
       0); 
     mListView.setAdapter(simpleCursorAdapter); 
    } 


    @Override //added following https://stackoverflow.com/a/27192897/5774375 
    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); 
    } 
} 

activi ty_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.myappshop.myapp.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

    <include layout="@layout/content_main" /> 

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

Die minimale API, die I'v eingestellt 15. ist ich auf ähnliche Antworten auf SO haben gesucht, wie this aber nicht mein Probelm lösen. So schätzen Sie Ihre Hilfe.

UPDATE: DetailActivity.java hinzugefügt:

public class DetailActivity extends AppCompatActivity { 
    private static final String TAG = DetailActivity.class.getSimpleName(); 

    Button shareBtn, nextBtn, prevBtn, indexBtn ; 
    DictionaryDatabase mDB; 
    TextView title, body; //Globally Declaration 
    String nextId; 
    int id; 

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

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



     try{ 
      String key = getIntent().getStringExtra("key"); 
      String value = getIntent().getStringExtra("value"); 
      String idString = getIntent().getStringExtra("nextId"); 
      id = Integer.parseInt(idString) + 1; //to be used to rener the next item 


      title = (TextView) findViewById(R.id.title); 
      title.setText(key); 

      body = (TextView) findViewById(R.id.body); 
      body.setText(value); 
     } 
     catch(Exception e){ 
      e.printStackTrace(); 
     } 

     final Button button = (Button) findViewById(R.id.shareBtn); 


     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // Perform action on click 
       Intent sendIntent = new Intent(); 
       sendIntent.setAction(Intent.ACTION_SEND); 
       sendIntent.putExtra(Intent.EXTRA_TEXT, body.getText().toString()); 
       sendIntent.setType("text/plain"); 
       startActivity(sendIntent); 
      } 
     }); 
     shareBtn = (Button) findViewById(R.id.shareBtn); 


     //render the next word 
     mDB = new DictionaryDatabase(this); 
     nextBtn = (Button) findViewById(R.id.nextBtn); 
     prevBtn = (Button) findViewById(R.id.prevBtn); 
     indexBtn = (Button) findViewById(R.id.indexBtn); 

     nextBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       id+=1; 
       Log.d(TAG, "the next item to be fetched has id: " + Long.toString(id)); 

       Log.d(TAG, "return value is: " + mDB.getWord(id)); 
       if (mDB.getWord(id) != "") { 
        String key = mDB.getWord(id); 
        String value = mDB.getDefinition(id); 

        title = (TextView) findViewById(R.id.title); 
        title.setText(key); 

        body = (TextView) findViewById(R.id.body); 
        body.setText(value); 
        prevBtn.setVisibility(View.VISIBLE); 

       } else { 
         finish(); 
       } 
      } 
     }); 

     prevBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       id-=1; 
       if (mDB.getWord(id) != "") { 
        String key = mDB.getWord(id); 
        String value = mDB.getDefinition(id); 

        title = (TextView) findViewById(R.id.title); 
        title.setText(key); 

        body = (TextView) findViewById(R.id.body); 
        body.setText(value); 
        nextBtn.setVisibility(View.VISIBLE); 

       } else { 
        finish(); 
       } 

      } 
     }); 

     indexBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       finish(); 

      } 
     }); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, 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); 
    } 
} 

Antwort

2

Sie haben Ihre menu.xml aufzublasen, bevor Sie seine Menüpunkte aufrufen können. Fügen Sie diese vor onOptionsItemSelected()

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

Sorry darüber. Ich habe auch bemerkt, dass du deine Toolbar in deiner Aktivität 'onCreate() nicht deklariert hast. Sie müssten Ihre Symbolleiste deklarieren, bevor Sie Menüelemente darauf platzieren. Fügen Sie dies der onCreate-Methode Ihrer Aktivität hinzu.

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

Stellen Sie außerdem sicher, dass Sie Ihr Toolbar-/Appbar-Layout in Ihrem Hauptlayout für diese Aktivität enthalten.

+0

Ich habe den obigen Code vor 'onOptionsItemSelected' hinzugefügt, aber das Element wurde nicht angezeigt. Zusätzlich zu dem jetzt, wenn ich jedes Wort anklicke, hängt die App. – Karlom

+0

@Karlom Ich habe meine Antwort bearbeitet –

+0

Ja, nach dem Hinzufügen dieser kann ich die Menüpunkte sehen. Vielen Dank! – Karlom