android工具栏菜单无法正常工作

时间:2015-04-27 16:33:44

标签: java android layout menu toolbar

我在填充工具栏菜单时出现问题。该应用只是停止工作'每当我进入工具栏所在的活动时。也许我错过了什么?

以下是活动代码:

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class ProjectCreateScreen extends ActionBarActivity {

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


    Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.AwesomeBar);
    setSupportActionBar(toolbar);

    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            // Handle the menu item
            return true;
        }
    });

    final TextView noProject = (TextView) findViewById(R.id.NOPROJECT);

    final ArrayList<String> listItems=new ArrayList<String>();
    final ListAdapter addAdapter = new ArrayAdapter<String>(this,
            R.layout.list_item, R.id.listFrame, listItems);
    final ListView lv = (ListView) findViewById(R.id.lv);
    lv.setAdapter(addAdapter);

    Button actualAdd = (Button) findViewById(R.id.addButton);
    Button actualPplAdd = (Button) findViewById(R.id.addPeople);
    actualAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            noProject.setVisibility(View.GONE);
            lv.setVisibility(View.VISIBLE);
            listItems.add("New Project");
            ((ArrayAdapter) addAdapter).notifyDataSetChanged();
        }
    });
    actualPplAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent switchToEdit = new Intent(ProjectCreateScreen.this,
                    teamCreateScreen.class);
            startActivity(switchToEdit);
        }
    });
}
public void displayProjectOptions (View view)  {
}
@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_primary, menu);
    return true;
}
}

活动使用的XML(工具栏在这里):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl">

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/actionBarSize"
    android:id="@+id/AwesomeBar"
    android:background="@color/custom_theme_color">
    </android.support.v7.widget.Toolbar>

<TextView
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text="@string/noProjectsNotice"
    android:id="@+id/NOPROJECT"
    android:gravity="center"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:textSize="16sp"/>

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/AwesomeBar"
    android:id="@+id/lv"
    android:visibility="invisible">
</ListView>

</RelativeLayout>

我正在使用的菜单:

<?xml version="1.0" encoding="utf-8"?>
<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=".ProjectCreateScreen">

<item android:id="@+id/addButton"
    android:icon="@drawable/ic_action_new"
    android:title="@string/AddProjectSmall"
    app:showAsAction="ifRoom"/>

<item android:id="@+id/addPeople"
    android:icon="@drawable/addpplactual"
    android:title="@string/AddPeople"
    app:showAsAction="ifRoom"/>

<item android:id="@+id/CL"
    android:icon="@drawable/ic_action_cloud"
    android:title="@string/FileStorage"
    app:showAsAction="ifRoom"/>

</menu>

0 个答案:

没有答案