0

我正在尝试在我的首选项活动中使用带有自定义 xml 的小吃吧。我做错了什么,因为我得到了一个空指针异常。我认为这与视图有关,或者可能是因为我不使用协调器布局。这是我的Java代码:

@Override
public boolean onPreferenceChange(Preference preference, final Object newValue) {
    String key = preference.getKey();
    if (key.equals("distance")) {
        preference.setSummary((String) newValue);
        Snackbar snackbar = Snackbar
                .make(findViewById(R.id.snackbarPosition), "You choose " + newValue, Snackbar.LENGTH_LONG)
                .setAction("UNDO", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (newValue.equals("km")){
                            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MyPrefs.this);
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("distance", "miles");
                            editor.commit();
                        }else if (newValue.equals("miles")){
                            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MyPrefs.this);
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("distance", "km");
                            editor.commit();

                        }
                    }
                });

        snackbar.show();

小吃吧 xml:

<?xml version="1.0" encoding="utf-8"?>

<View
    android:id="@+id/sb__divider"
    android:layout_width="match_parent"
    android:layout_height="3dp" />

<LinearLayout
    android:id="@+id/sb__inner"
    android:background="@color/transparent_black_percent_70"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <TextView
        android:id="@+id/sb__text"
        style="@style/Snackbar.Text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="You choose blah blah"
        android:ellipsize="end"
        android:focusable="true" />

    <Button
        android:id="@+id/sb__action"
        style="@style/Snackbar.Text.Action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="undo"
        android:gravity="right"
        android:focusable="true" />

</LinearLayout>

偏好活动:

<?xml version="1.0" encoding="utf-8"?>

<ListPreference
    android:dialogTitle="Select a distance system"
    android:entries="@array/distanceEntries"
    android:entryValues="@array/distanceValues"
    android:key="distance"
    android:title="Distance" />
4

3 回答 3

5

要在 Preferences Activity 中获取父布局,应该使用 getListView()。从这里: https ://stackoverflow.com/a/8432096/5342189

于 2015-09-24T13:42:03.517 回答
0

尝试这个:

  Snackbar snackbar = Snackbar
                            .make(coordinatorLayout, "Had a snack at Snackbar", Snackbar.LENGTH_LONG)
                            .setAction("Undo", mOnClickListener);
                    snackbar.setActionTextColor(Color.WHITE);
                    View snackbarView = snackbar.getView();
                    snackbarView.setBackgroundColor(Color.DKGRAY);
                    TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
                    textView.setTextColor(Color.YELLOW);
                    snackbar.show();
于 2015-09-24T11:37:00.970 回答
-1

Gigi Sprintzin 的链接提供了在首选项活动中显示 Snack Bar 所需的答案。我已经根据您的确切问题调整了链接代码:

@Override
public boolean onPreferenceChange(Preference preference, final Object newValue) {
    View root = SettingsPreferenceScreen.this.getListView();
    String key = preference.getKey();
    if (key.equals("distance")) {
        preference.setSummary((String) newValue);
        Snackbar snackbar = Snackbar
                .make(root, "You choose " + newValue, Snackbar.LENGTH_LONG)
                .setAction("UNDO", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (newValue.equals("km")){
                            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MyPrefs.this);
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("distance", "miles");
                            editor.commit();
                        }else if (newValue.equals("miles")){
                            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MyPrefs.this);
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("distance", "km");
                            editor.commit();

                        }
                    }
                });

        snackbar.show();
    }
}

当然,您必须更改View root = SettingsPreferenceScreen.this.getListView();为使用您的首选项类的名称。

于 2017-05-22T03:15:09.003 回答