0

I've come across a problem that I have searched and searched for a solution for, and cannot find one. I am trying to set up a Preference screen which is opened by an action button on an ActionBarSherlock action bar. The buttons appear on the action bar, but they don't do anything when pressed. They are supposed to start a new intent, but it doesn't. Here's some code:

Main.java:

package com.myname.minteract;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import android.content.Intent;
import android.os.Bundle;

public class Main extends SherlockFragmentActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.activity_main, menu);
        menu.add("Add Server")
        .setIcon(R.drawable.ic_action_add_server)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menu.add("Refresh Servers")
        .setIcon(R.drawable.ic_action_refresh)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menu.add("Settings")
        .setIcon(R.drawable.ic_action_options)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return(super.onCreateOptionsMenu(menu));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.addServer:
            startActivity(new Intent(this, AddServer.class));
            return(true);
        case R.id.settings:
            startActivity(new Intent(this, Settings.class));
            return(true);
        }
        return(super.onOptionsItemSelected(item));
    }
}

My other class files and xml files are identical to the ones found here, with minor name changes (posting all of my code would take up an awful lot of space).

Hopefully this is enough information. I am fairly new to Android and very new to ActionBarSherlock, so I may be missing something fairly easy. I've tried using logcat and such, but the button literally does nothing, it doesn't even show up on the logcat with an error.

Thanks!

Extra note: I am testing my app using a HTC Sensation 4G running ICS and a MyTouch 3G running Froyo (to make sure it is compatible with pre-honeycomb devices).

EDIT:

Here's my /res/menu/activity_main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_settings"
        android:title="@string/menu_settings"
        android:orderInCategory="100"
        android:showAsAction="never" />
    <item android:id="@+id/addServer"
        android:icon="@drawable/ic_action_add_server"
        android:title="Add Server" />
    <item android:id="@+id/refreshServers"
        android:icon="@drawable/ic_action_refresh"
        android:title= "Refresh Servers" />
    <item android:id="@+id/settings"
        android:icon="@drawable/ic_action_options"
        android:title="Settings" />
</menu>
4

1 回答 1

3

您已经在 XML 文件中设置了所有内容的 ID,并在那里设置了其他所有内容,因此无需menu.add(...)在主代码中使用。只需充气Menu,一切都会好起来的。

此外,由于您已经处理了该事件,请true改为返回。尝试这个:

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

我个人建议将这些添加为<item ...>sres/menu/activity_main.xml而不是以编程方式添加,但如果这对您有用,那么这很重要,对吧?

于 2012-08-04T03:35:54.947 回答