【问题标题】:Settings activity doesn't open on tablet设置活动未在平板电脑上打开
【发布时间】:2015-06-26 13:29:02
【问题描述】:

我已经正确设置了 pref_general、它的值以及运行意图的代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        Intent intecao = new Intent(characterList.this, SettingsActivity.class);
        startActivity(intecao);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

因此,如果我在选项弹出窗口中点击设置,它将在我的手机/7 英寸平板电脑中正确打开,但对于更大的平板电脑,例如 9 英寸或更大,它会给我一个空白屏幕弹出窗口,没有任何内容它,例如:

我该如何解决这个问题?导致这种情况发生的问题是什么?

--- 编辑 ---

这是我的 pref_general:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="Made by Megafuji and Kintups"/>

    <ListPreference
        android:key="console1"
        android:title="Console"
        android:summary="Select your console"
        android:defaultValue="1"
        android:entries="@array/pref_example_list_titles"
        android:entryValues="@array/pref_example_list_values"
        android:negativeButtonText="@null"
        android:positiveButtonText="@null" />

    <ListPreference
        android:key="viewMode"
        android:title="View Mode"
        android:summary="Select your View Mode"
        android:defaultValue="1"
        android:entries="@array/pref_viewMode_list_titles"
        android:entryValues="@array/pref_viewMode_list_values"
        android:negativeButtonText="@null"
        android:positiveButtonText="@null" />

    <ListPreference
        android:key="charsize"
        android:title="Icons Size"
        android:summary="Select character screen size for text and icons."
        android:defaultValue="1"
        android:entries="@array/pref_character_size"
        android:entryValues="@array/pref_character_size_value"
        android:negativeButtonText="@null"
        android:positiveButtonText="@null" />

    <ListPreference
        android:key="tamanhoLetra"
        android:title="Font Size"
        android:summary="Select font size you want for move list."
        android:defaultValue="1"
        android:entries="@array/pref_font_size"
        android:entryValues="@array/pref_font_size_value"
        android:negativeButtonText="@null"
        android:positiveButtonText="@null" />

    <CheckBoxPreference
        android:key="screenDimming"
        android:title="Disable screen dimming in this app"
        android:summary="Disable screen locking/turning off."
        android:defaultValue="false"
        />

    <CheckBoxPreference
        android:key="frame"
        android:title="Show Frame Date"
        android:summary="Show Frame Data for All Moves "
        android:defaultValue="false"
        />

    <CheckBoxPreference
        android:key="damage"
        android:title="Show Damage"
        android:summary="Show Damage for All Moves "
        android:defaultValue="false" />

    <PreferenceCategory
        android:title="Contact us: mkxmoves@gmail.com"/>
</PreferenceScreen>

【问题讨论】:

  • 什么是characterList.this?告诉我们SettingsActivity的来源。
  • characterList.this 是使用的上下文,SettingsActivity 不存在,我已经显示了 pref_general

标签: android android-settings


【解决方案1】:

找到答案了!

Android Developer Settings 之后,我需要做的就是如果我正在为 Android 3.0(API 级别 11)及更高版本进行开发,我需要使用 Preference Fragments,它是另一个扩展 PreferenceFragment 的类。

所以代码将使用这些参数:

public static class SettingsFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }
    ...
}

而且旧的 SettingsActivity 现在必须扩展 Activity 而不是 PreferenceActivity,如下:

public class SettingsActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();
    }
}

【讨论】:

    【解决方案2】:
                public class SettingActivity extends PreferenceActivity
                {
                    @Override
                    protected void onCreate(Bundle savedInstanceState) 
                    {
                        setTheme(android.R.style.Theme_Holo_Light_DarkActionBar);
                        requestWindowFeature(Window.FEATURE_ACTION_BAR);
                        super.onCreate(savedInstanceState);
    
                        FragmentManager mFragmentManager = getFragmentManager();
                        FragmentTransaction mFragmentTransaction = mFragmentManager
                                .beginTransaction();
                        PrefsFragment mPrefsFragment = new PrefsFragment();
                        mFragmentTransaction.replace(android.R.id.content, mPrefsFragment);
                        mFragmentTransaction.commit();
                    }
    
                   public static class PrefsFragment extends PreferenceFragment {
    
                    @Override
                    public void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
    
                        // Load the preferences from an XML resource
                        addPreferencesFromResource(R.xml.preferences);
                        String userName;
                        String tmpemail;
    
                        findPreference("profile_setting_username").setTitle(userName);
                        findPreference("profile_setting_username").setSummary(strHtml);
                     }
                 }
              }
    
    This is how I have implemeted my Settings Activity and it works on all type of devices.
    

    【讨论】:

      【解决方案3】:

      您的 XML 中的平板电脑设置视图很可能没有您的实际设置视图元素。

      您可以阅读Android docs 了解有关支持多个视图的更多信息。基本上,自动设置的规则导致使用平板电脑而不是手机的 XML 视图。

      它应该看起来像这样:

      res/layout/my_layout.xml             // layout for normal screen size ("default")
      res/layout-large/my_layout.xml       // layout for large screen size
      res/layout-xlarge/my_layout.xml      // layout for extra-large screen size
      res/layout-xlarge-land/my_layout.xml // layout for extra-large in landscape orientation
      

      要么删除 XML 文件,要么更新它以包含所需的设置元素。

      【讨论】:

      • 感谢您的快速答复。我没有为设置设置任何活动,因为它全部在没有任何平板电脑标签的 XML/pref_general.xml 中完成。
      • 能否在问题中包含 pref_general.xml 的内容和 android 清单?
      猜你喜欢
      • 2013-06-12
      • 1970-01-01
      • 2023-04-08
      • 2020-04-28
      • 2017-06-18
      • 2018-10-18
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多