将支持操作栏添加到现有应用程序:未显示任何菜单项

时间:2014-07-15 00:57:06

标签: android android-actionbar android-actionbar-compat

我已经使用minSDK 14实现了一个非常棒的应用程序。现在我试图“降级”到minSDK 10只是为了看看会发生什么。

我修复了GridLayoutSwitch之类的内容,但我遇到的问题是ActionBar不再显示任何内容。

我已将我的MainActivity从FragmentActivity更改为ActionBarActivity。然后我将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;
}

1 个答案:

答案 0 :(得分:1)

在您的片段中,您需要通知setHasOptionsMenu有行动项目。否则,可能无法调用onCreateOptionsMenu。我使用ABC(以前的ActionBarSherlock)和类似的FragmentTabPager概念,我需要在片段中使用它:

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

    setHasOptionsMenu(true);
}