Android操作栏显示没有项目

时间:2016-05-22 23:50:54

标签: android android-actionbar

我是Android新手并且遇到了这个问题:操作栏上没有项目,如下图所示:

enter image description here

这是我的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>

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);
    }
}

activity_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>

我设置的最小API是15。 我在SO上看过类似的答案,比如this,但没有解决我的问题。非常感谢你的帮助。

更新:添加了DetailActivity.java:

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);
    }
}

1 个答案:

答案 0 :(得分:2)

在调用菜单项之前,必须先膨胀menu.xml。在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;
    }

很抱歉。我还注意到你没有在你的活动中宣布你的工具栏。的onCreate()。在将菜单项放在其上之前,您必须声明工具栏。将此添加到您的活动的onCreate方法。

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

此外,请确保在此主题布局中包含此工具栏的工具栏/应用栏布局。