在Android中创建了一个菜单,但菜单不可见

时间:2015-04-07 12:17:48

标签: java android

我在我的android-app中创建了一个菜单,但是当我在手机上测试我的应用程序时,菜单不可见。这是为什么?

这是我的menu.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=".MainActivity">

    <item  android:id="@+id/item1"
        android:title="Item 1"/>
    <item  android:id="@+id/item2"
        android:title="Item 2"/>
    <item  android:id="@+id/item3"
        android:title="Item 3"/>

</menu>

这是我的MainActivity中的onOptionItemSelected方法:

protected boolean onOptionItemSelected (MenuItem item) {
    switch (item.getItemId()) {
        case R.id.item1:
            Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show();
            return true;
        case R.id.item2:
            Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show();
            return true;
        case R.id.item3:
            Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }

}

我的onCreateOptionMenu:

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

我错过了什么?

编辑。我添加了忘记的onCreateOptionMenu。菜单现在可见,但onOptionItemSelected方法不起作用。当我选择任何一个选项时,什么也没发生。

此处完整活动:

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.net.Uri;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.content.Intent;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements SensorEventListener {

    private ImageView compassImage;

    private float initialDegree = 0f;
    private float currentDegree;
    private SensorManager manager;

    TextView infoTextAboutDegrees;

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

        Button startBtn = (Button) findViewById(R.id.makeCall);
        startBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                emergencyCall();
            }
        });


        compassImage = (ImageView) findViewById(R.id.imageViewCompass);

        infoTextAboutDegrees = (TextView) findViewById(R.id.tvHeading);

        manager = (SensorManager) getSystemService(SENSOR_SERVICE);


    }

    @Override
    protected void onResume() {
        super.onResume();

        manager.registerListener(this, manager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onPause() {
        super.onPause();

        manager.unregisterListener(this);
    }

    public void emergencyCall() {

        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:911"));

        try {
            startActivity(intent);
            finish();
            Log.i("Call success...", "");
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MainActivity.this,"Call fail", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onSensorChanged(SensorEvent event) {


        currentDegree = Math.round(event.values[0]);

        infoTextAboutDegrees.setText("Current degrees: " + Float.toString(currentDegree));

        RotateAnimation rotateAnimation = new RotateAnimation(
                initialDegree,
                -currentDegree,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f);

        rotateAnimation.setDuration(210);

        rotateAnimation.setFillAfter(true);

        compassImage.startAnimation(rotateAnimation);
        initialDegree = -currentDegree;

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {


    }

    protected boolean onOptionItemSelected (MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item1:
                Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show();
                return true;
            case R.id.item2:
                Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show();
                return true;
            case R.id.item3:
                Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }


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

}

4 个答案:

答案 0 :(得分:0)

覆盖onCreateOptionsMenu()并使该方法中的菜单膨胀。

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

有关详细信息,请参阅http://developer.android.com/guide/topics/ui/menus.html#options-menu

答案 1 :(得分:0)

您确定,您在代码下方集成了创建选项菜单。

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

如果您正在使用片段,请使用此:

setHasOptionsMenu(true);

我希望它能奏效。

从菜单xml中删除代码:

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"

答案 2 :(得分:0)

你有一个错字 - 总是添加@Override然后你自己看到它:

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    return super.onOptionsItemSelected(item);
}

答案 3 :(得分:0)

@覆盖 public boolean onCreateOptionsMenu(菜单菜单){...}

没有受到保护。