在现有应用程序中添加支持操作栏:不显示菜单项



我已经实现了一个应用程序与minSDK 14的工作只是伟大的。现在我正试着"降级"到minSDK 10,看看会发生什么。

我有固定的东西,如GridLayoutSwitch,但我有一个问题,ActionBar不显示任何项目了。

我已经改变了我的MainActivity从FragmentActivityActionBarActivity。然后我把getActionBar()改成了getSupportActionBar()。之后,我将清单文件中的主题样式更改为几个东西,只是为了看看是否发生了什么。现在我卡住了,有什么东西不见了吗?

public class MainActivity extends ActionBarActivity {
/** Called when the activity is first created. */
// private BluetoothAdapter mBluetoothAdapter = null;
public static String sAddress = "00:00:00:00:00:00"; // BoxAdresse
public AlertDialog.Builder b;
public BluetoothConnection bc = new BluetoothConnection();
// Debugging
private static final String TAG = "Main_Activity";
private static final boolean D = true;
// Message types sent from the BluetoothChatService Handler
public static final int MESSAGE_STATE_CHANGE = 1;
public static final int MESSAGE_READ = 2;
public static final int MESSAGE_WRITE = 3;
public static final int MESSAGE_DEVICE_NAME = 4;
public static final int MESSAGE_TOAST = 5;
public static final String TOAST = "toast";
// Key names received from the BluetoothChatService Handler
public static final String DEVICE_NAME = "device_name";
// Intent request codes
public static final int REQUEST_CONNECT_DEVICE_SECURE = 1;
private static final int REQUEST_ENABLE_BT = 3;
private static int CONNECT_COUNTER = 0;
private static final int MAX_CONNECT_ATTEMPT = 4;
private static final String TAG_ADMIN = "user_admin";
private static final String TAG_STANDARD = "user_standard";
private static String adminPermission;
// Name of the connected device
public static String mConnectedDeviceName = null;
// Local Bluetooth adapter
private static BluetoothAdapter mBluetoothAdapter = null;
// Member object for the chat services
public static BluetoothService mSerialService = null;
private static String mInStringBuffer;
public static Handler handler = new Handler(); // for postDelayed
private ActionBar actionBar;
public static Menu mMenu;
private Byte_Translation bWork;
public static FragmentActivity thisFragmentActivity = null;
private FragmentTabHost mTabHost;
private View tabIndicator1, tabIndicator2, tabIndicator3, tabIndicator4;
private TextView title;
private CharSequence mTitle;
String[] mFragments = {
    "de.teamgrimm.android.crohmapp.tabs.InformationTabs",
    "de.teamgrimm.android.crohmapp.tabs.DevelopmentTab",
    "de.teamgrimm.android.crohmapp.tabs.ChargingTab" };
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bottom_tabs);
    actionBar = getSupportActionBar();
    mTitle = getTitle();
    // set the Icon
    actionBar.setIcon(R.drawable.ic_launcher);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
    tabIndicator1 = LayoutInflater.from(this).inflate(
        R.layout.crohmapptheme_tab_indicator_holo,
        mTabHost.getTabWidget(), false);
    title = (TextView) tabIndicator1.findViewById(android.R.id.title);
    title.setText("Home");
    tabIndicator2 = LayoutInflater.from(this).inflate(
        R.layout.crohmapptheme_tab_indicator_holo,
        mTabHost.getTabWidget(), false);
    title = (TextView) tabIndicator2.findViewById(android.R.id.title);
    title.setText("Infos");
    tabIndicator3 = LayoutInflater.from(this).inflate(
        R.layout.crohmapptheme_tab_indicator_holo,
        mTabHost.getTabWidget(), false);
    title = (TextView) tabIndicator3.findViewById(android.R.id.title);
    title.setText("Charging");
    tabIndicator4 = LayoutInflater.from(this).inflate(
        R.layout.crohmapptheme_tab_indicator_holo,
        mTabHost.getTabWidget(), false);
    title = (TextView) tabIndicator4.findViewById(android.R.id.title);
    title.setText("Development");
    mTabHost.addTab(
        mTabHost.newTabSpec("home").setIndicator(tabIndicator1),
        HomeFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("infos")
        .setIndicator(tabIndicator2), InformationTabs.class, null);
    mTabHost.addTab(
        mTabHost.newTabSpec("charging").setIndicator(tabIndicator3),
        ChargingTabs.class, null);
    mTabHost.addTab(
        mTabHost.newTabSpec("development").setIndicator(tabIndicator4),
        DevelopmentTabs.class, null);
    thisFragmentActivity = this;
    bWork = new Byte_Translation(this, mHandler);
    InOutFragment.mOutgoingArrayAdapter = new ArrayAdapter<String>(this,
        R.layout.message);
    InOutFragment.mIncomingArrayAdapter = new ArrayAdapter<String>(this,
        R.layout.message);
    ManualDebugFragment.mReceivedHexArrayAdapter = new ArrayAdapter<String>(
        this, R.layout.message);
    ManualDebugFragment.mSentHexArrayAdapter = new ArrayAdapter<String>(
        this, R.layout.message);
    DebugFragment.debugArrayAdapter = new ArrayAdapter<String>(this,
        R.layout.message);
    // // Get local Bluetooth adapter
    // mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    //
    // // If the adapter is null, then Bluetooth is not supported
    // if (mBluetoothAdapter == null) {
    //
    // Toast.makeText(this, "Bluetooth is not available",
    // Toast.LENGTH_LONG).show();
    // finish();
    // return;
    // }
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // // Take appropriate action for each action item click
    // if (menuItem.get_mItem(item.getItemId()) != null) {
    // switch (menuItem.get_mItem(item.getItemId())) {
    // Take appropriate action for each action item click
    if (item != null) {
        switch (item.getItemId()) {
        case (R.id.connect_box):
            if (!Byte_Translation.isConnected()) {
                devicedialog();
            } else {
                if (mSerialService != null)
                    mSerialService.stop();
            }
            return true;
        case (R.id.disconnect_box):
            if (Byte_Translation.isConnected()) {
                // Stop BT Service to disconnect
                if (mSerialService != null)
                    mSerialService.stop();
                    sAddress = "00:00:00:00:00:00";
                }
            return true;
        case (R.id.login_box):
            Byte_Translation.LOGIN_SUCCESSFULL = false;
            Byte_Translation.LOGINCOUNT = 0;
            Byte_Translation.login();
            return true;
        case (R.id.refresh_menu):
            Byte_Translation.refresh_all();
            Byte_Translation.refresh_timer();
            return true;
        case (R.id.auto_refresh):
            if (!Byte_Translation.AUTOREFRESH) {
                Byte_Translation.AUTOREFRESH = true;
                item.setChecked(true);
                Byte_Translation.refresh();
            } else {
                Byte_Translation.AUTOREFRESH = false;
                item.setChecked(false);
            }
            save_shared_Prefs();
            return true;
        case (android.R.id.home):
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                MainActivity.this);
            alertDialogBuilder.setTitle("Exit " + this.getTitle());
            alertDialogBuilder.setMessage("Are you sure?");
            // set positive button: Yes message
            alertDialogBuilder.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        onBackPressed();
                    }
                });
            // set negative button: No message
            alertDialogBuilder.setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
            });
            AlertDialog alertDialog = alertDialogBuilder.create();
            // show alert
            alertDialog.show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    return false;
}

在您的片段中,您需要通知setHasOptionsMenu中有操作项。否则,onCreateOptionsMenu可能不会被调用。我使用ABC(以前是ActionBarSherlock)和类似的FragmentTabPager概念,并且我需要在fragments中使用它:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setHasOptionsMenu(true);
}

最新更新