无法让屏幕显示设置选项

Can't get screen to show settings options

EDIT 在底部添加 MyActivity.java(即主要 activity) EDIT2 添加行到 MyActivity.java (这解决了问题)

我设置了首选项,但无法访问它们。无论我在 xml 中选择什么 style,无论我在 Android Studio (AS) 1.1.0 中选择什么虚拟设备或样式,屏幕都缺少如下所示的 3 个点。甚至包括 LightActionBarDarkActionBar 的下拉样式也没有显示这些点。

在 xml 中,我尝试了 <style name="AppBaseTheme" parent="android:Holo.ButtonBar">,它昨晚终于在一个小应用程序上运行了(遇到同样的问题),而且,对于 parent,我尝试了Base.Theme.AppCompat.Light.DarkActionBar 和其他东西。

如果我看到 3 个点,我不太在意;只需公开首选项屏幕即可。

我还尝试了 neverifroomalways showAsAction:

<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=".MyActivity">

<item android:id="@+id/itemFocus"
      android:title="@string/focusAtClue"
      android:orderInCategory="200"
      app:showAsAction="never"/>

这里是preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        >
        <CheckBoxPreference
            android:key="@string/focusAfterShow"
            android:title="@string/focusAfterShow"
            android:summary="Always place the cursor at the 'clue' (sum) after tapping 'Show'."
            android:defaultValue="true"
            />
    </PreferenceCategory>
    <PreferenceCategory
        >
        <CheckBoxPreference
            android:key="@string/screenSaver"
            android:title="@string/screenSaver"

            android:summary="Keep screen on at all times while running this app."
            android:defaultValue="true"
            />
    </PreferenceCategory>

</PreferenceScreen>

这里是SettingsFragment.java

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.util.Log;

public class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
  }
  @Override
  public void onResume() {
    super.onResume();
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
  }
  @Override
  public void onPause() {
    super.onPause();
    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
  }
  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
    if (key.equalsIgnoreCase("pie_type")){
      Log.w("Settings", sharedPref.getString(key, ""));
    }
  }
}

SettingsActivity.java

    import android.app.Activity;
    import android.os.Bundle;

public class SettingsActivity extends Activity {
  public static final String SETTINGS = "com.whatever.kakurocombosbuildvariants.settings";
  public static final String FIRST_USE = "com.whateverkakurocombosbuildvariants.firstUse";

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

}

这是在 MyActivity.java 中调用 SettingsActivity 的地方:

public boolean onOptionsItemSelected(MenuItem item)
  {
    switch (item.getItemId()) {
      case R.id.menu_settings:
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);

        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

MyActivity.java(主要 activity;删除了 300 行无关代码)

public class MyActivity extends Activity {

  public final String
      prefix = "com.XXXX.kakurocombosbuildvariants"
      , SETTINGS =        prefix + ".settings"
      , FIRST_USE =       prefix + ".firstUse"
      , FOCUS_AT_CLUE =   prefix + ".focusAtClue"
      , SCREENSAVER =     prefix + ".screensaver"
      , literally_Focus_At_Clue = "Focus at clue"
      , literally_Screen_saver  = "Screen saver"
      ;
  public boolean firstUse;

  SharedPreferences preferences;
  SharedPreferences.Editor editor;

  boolean screenSaver;//= false;
  boolean focusAtClue ;//= true;

  AlertDialog alertDialog;

  private void makeActionOverflowMenuShown() {
    //devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu
    try {
      ViewConfiguration config = ViewConfiguration.get(this);
      Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
      if (menuKeyField != null) {
        menuKeyField.setAccessible(true);
        menuKeyField.setBoolean(config, false);
      }
    } catch (Exception e) {
      popupMessage("Problem making actionbar overflow");
    }
  }

  void showKeypad(){
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
  }

  public static boolean isTablet(Context ctx){
    return (ctx.getResources().getConfiguration().screenLayout
        & Configuration.SCREENLAYOUT_SIZE_MASK
    )
        >= Configuration.SCREENLAYOUT_SIZE_LARGE;
  }

  @Override public boolean onPrepareOptionsMenu(Menu menu)
  {
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch (item.getItemId()) {
      case R.id.menu_settings:
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);

        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  private void setScreensaver()
  {
    if( ! screenSaver) getWindow().addFlags  (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    else               getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  }

  @Override protected void
  onCreate(Bundle savedInstanceState) // ************************** ON CREATE **********
  {
    super.onCreate(savedInstanceState);

/////////////////////////// EDIT2 ///////////////////////////////////////    

    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getWindow().setFormat(Window.FEATURE_ACTION_BAR);

/////////////////////////// EDIT2 ///////////////////////////////////////    

    if(! FREE) setContentView(R.layout.activity_my);
    else       setContentView(R.layout.activity_free);

    SharedPreferences preferences = getSharedPreferences(SETTINGS, MODE_PRIVATE);

    firstUse = preferences.getBoolean(FIRST_USE, true);
    if(firstUse){
      Toast.makeText(getApplicationContext(), "Welcome to Kakuro Combos", Toast.LENGTH_SHORT).show();
      editor = preferences.edit();
      editor.putBoolean(FIRST_USE, false);
      editor.commit();
    }

    alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "OK",
                          new DialogInterface.OnClickListener() { public void
                                                                  onClick(DialogInterface dialog, int which)
                          {
                            dialog.dismiss();
                          }});
    showKeypad();

    makeActionOverflowMenuShown();

    getWindow().setFormat(Window.FEATURE_ACTION_BAR);

    showKeypad();

    setScreensaver();


  } // onCreate
}

////////////////////// 编辑2 //////////////////// ///////

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

////////////////////// 编辑2 //////////////////// ///////

看来主要问题是您没有扩充菜单 xml。

尝试为您的 MainActivity 使用 ActionBarActivity,并添加 onCreateOptionsMenu() 以扩充菜单 xml。

public class MyActivity extends ActionBarActivity{

    //...........

    @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_main, menu);
        return true;
    }

    //............
}

您需要加载菜单:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.<your_menu>, menu);
    //...
}