在偏好的情况下显示Snackbar

时间:2016-06-21 12:35:01

标签: java android sharedpreferences android-view android-snackbar

我想在我的应用程序中添加一个基本的快餐栏,但是我收到了一个错误,我无法弄清楚为什么。 我认为问题是getView(),可能是我必须使用getListView()但是我在静态上下文中这样怎么样?

这是我的Java代码:

public class SettingsFragment extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction().replace(android.R.id.content,
                new PrefsFragment()).commit();
        ActionBar bar = getActionBar();
        bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#4CAF50")));

    }


    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.settings);

            Preference save_database = findPreference("savedatabase");
            save_database.setOnPreferenceClickListener( new Preference.OnPreferenceClickListener()
            {
                public boolean onPreferenceClick( Preference pref )
                {
                    Data.bdd.Save_database(getActivity(),getView());
                    return true;
                }
            } );



            final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("checkbox_always_on");
            checkboxPref.
                    setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                        public boolean onPreferenceChange(Preference preference, Object newValue) {
                            if (newValue.toString().equals("true")) {

                                Screen.screen_on(getActivity());
                                Snackbar.make(getListView(),"Auto-extinction de l'écran désactivé",Snackbar.LENGTH_SHORT);

                            } else {
                                Screen.screen_off(getActivity());
                                Snackbar.make(getView(),"Auto-extinction de l'écran activé",Snackbar.LENGTH_SHORT);
                            }
                            return true;
                        }
                    });
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = super.onCreateView(inflater, container, savedInstanceState);
            view.setBackgroundColor(getResources().getColor(R.color.green_800));

            return view;
        }
    }

Graddle依赖项:

    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.android.support:support-v13:23.0.0'
    compile 'com.android.support:support-v4:23.0.0'
    compile 'com.android.support:design:23.0.0'
    compile 'com.android.support:recyclerview-v7:23.0.0'

我有这个错误,我不明白

android.view.InflateException: Binary XML file line #18: Error inflating class android.support.design.widget.Snackbar$SnackbarLayout
at android.view.LayoutInflater.createView(LayoutInflater.java:640)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
at android.view.LayoutInflater.inflate(LayoutInflater.java:483)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.support.design.widget.Snackbar.<init>(Snackbar.java:183)
at android.support.design.widget.Snackbar.make(Snackbar.java:206)
at com.***.SettingsFragment$PrefsFragment$2.onPreferenceChange(SettingsFragment.java:87)
at android.preference.Preference.callChangeListener(Preference.java:1076)
at android.preference.TwoStatePreference.onClick(TwoStatePreference.java:64)
at android.preference.Preference.performClick(Preference.java:1131)
at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:225)
at android.widget.AdapterView.performItemClick(AdapterView.java:334)
at android.widget.AbsListView.performItemClick(AbsListView.java:1531)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3667)
at android.widget.AbsListView$3.run(AbsListView.java:5590)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:614)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:483) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
at android.support.design.widget.Snackbar.<init>(Snackbar.java:183) 
at android.support.design.widget.Snackbar.make(Snackbar.java:206) 
at com.***.SettingsFragment$PrefsFragment$2.onPreferenceChange(SettingsFragment.java:87) 
at android.preference.Preference.callChangeListener(Preference.java:1076) 
at android.preference.TwoStatePreference.onClick(TwoStatePreference.java:64) 
at android.preference.Preference.performClick(Preference.java:1131) 
at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:225) 
at android.widget.AdapterView.performItemClick(AdapterView.java:334) 
at android.widget.AbsListView.performItemClick(AbsListView.java:1531) 
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3667) 
at android.widget.AbsListView$3.run(AbsListView.java:5590) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5944) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184) 
Caused by: android.view.InflateException: Binary XML file line #34: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:640)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.inflate(LayoutInflater.java:480)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.design.widget.Snackbar$SnackbarLayout.<init>(Snackbar.java:629)
at java.lang.reflect.Constructor.newInstance(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
at android.view.LayoutInflater.createView(LayoutInflater.java:614) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:483) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
at android.support.design.widget.Snackbar.<init>(Snackbar.java:183) 
at android.support.design.widget.Snackbar.make(Snackbar.java:206) 
at com.***.SettingsFragment$PrefsFragment$2.onPreferenceChange(SettingsFragment.java:87) 
at android.preference.Preference.callChangeListener(Preference.java:1076) 
at android.preference.TwoStatePreference.onClick(TwoStatePreference.java:64) 
at android.preference.Preference.performClick(Preference.java:1131) 
at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:225) 
at android.widget.AdapterView.performItemClick(AdapterView.java:334) 
at android.widget.AbsListView.performItemClick(AbsListView.java:1531) 
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3667) 
at android.widget.AbsListView$3.run(AbsListView.java:5590) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5944) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:614)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:480) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
at android.support.design.widget.Snackbar$SnackbarLayout.<init>(Snackbar.java:629) 
at java.lang.reflect.Constructor.newInstance(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
at android.view.LayoutInflater.createView(LayoutInflater.java:614) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:483) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
at android.support.design.widget.Snackbar.<init>(Snackbar.java:183) 
at android.support.design.widget.Snackbar.make(Snackbar.java:206) 
at com.***.SettingsFragment$PrefsFragment$2.onPreferenceChange(SettingsFragment.java:87) 
at android.preference.Preference.callChangeListener(Preference.java:1076) 
at android.preference.TwoStatePreference.onClick(TwoStatePreference.java:64) 
at android.preference.Preference.performClick(Preference.java:1131) 
at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:225) 
at android.widget.AdapterView.performItemClick(AdapterView.java:334) 
at android.widget.AbsListView.performItemClick(AbsListView.java:1531) 
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3667) 
at android.widget.AbsListView$3.run(AbsListView.java:5590) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5944) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184) 
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 5
at android.content.res.TypedArray.twGetColorStateList(TypedArray.java:438)
at android.widget.TextView.<init>(TextView.java:1388)
at android.widget.Button.<init>(Button.java:115)
at android.widget.Button.<init>(Button.java:108)
at android.widget.Button.<init>(Button.java:104)
at java.lang.reflect.Constructor.newInstance(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
at android.view.LayoutInflater.createView(LayoutInflater.java:614) 
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:480) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:366) 
at android.support.design.widget.Snackbar$SnackbarLayout.<init>(Snackbar.java:629) 
at java.lang.reflect.Constructor.newInstance(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
at android.view.LayoutInflater.createView(LayoutInflater.java:614) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:483) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:415) 
at android.support.design.widget.Snackbar.<init>(Snackbar.java:183) 
at android.support.design.widget.Snackbar.make(Snackbar.java:206) 
at com.***.SettingsFragment$PrefsFragment$2.onPreferenceChange(SettingsFragment.java:87) 
at android.preference.Preference.callChangeListener(Preference.java:1076) 
at android.preference.TwoStatePreference.onClick(TwoStatePreference.java:64) 
at android.preference.Preference.performClick(Preference.java:1131) 
at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:225) 
at android.widget.AdapterView.performItemClick(AdapterView.java:334) 
at android.widget.AbsListView.performItemClick(AbsListView.java:1531) 
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3667) 
at android.widget.AbsListView$3.run(AbsListView.java:5590) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5944) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184) 

编辑:我的风格

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    android:pre


    <PreferenceCategory
        android:title="General">

        <CheckBoxPreference
            android:key="checkbox_always_on"
            android:title="Garder l'écran allumé"
            android:defaultValue="false"/>


    </PreferenceCategory>

    <PreferenceCategory
        android:title="Base de donnée">

        <!--<ListPreference-->
            <!--android:key="list_preference"-->
            <!--android:title="General"-->
            <!--android:dialogTitle="General"-->
            <!--android:entries="@array/entries_list_preference"-->
            <!--android:entryValues="@array/entryvalues_list_preference" />-->

        <!--<Preference-->
            <!--android:title="General">-->
            <!--<intent android:action="android.intent.action.VIEW"-->
                <!--android:data="http://codepath.com/" />-->
        <!--</Preference>-->

        <Preference
            android:title="Sauvegarder la base de donnée sur la carte SD"
            android:key="savedatabase">

        </Preference>


    </PreferenceCategory>

</PreferenceScreen>

3 个答案:

答案 0 :(得分:2)

所以在@YasinKaçmaz的帮助下,我找到了问题的答案。这是我的代码

public class SettingsFragment extends AppCompatActivity  {

static CoordinatorLayout coordinatorLayout;
 Activity thisActivity=this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SnackbarShow.showSnackbar(thisActivity);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction().replace(android.R.id.content,
                new PrefsFragment()).commit();
        ActionBar bar = getSupportActionBar();
       bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#4CAF50")));


    }

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

            addPreferencesFromResource(R.xml.settings);


            Preference save_database = findPreference("savedatabase");
            save_database.setOnPreferenceClickListener( new Preference.OnPreferenceClickListener()
            {
                public boolean onPreferenceClick( Preference pref )
                {
                    Data.bdd.Save_database(getActivity(),getView());
                    return true;
                }
            } );


            final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("checkbox_always_on");
            checkboxPref.
                    setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

                        public boolean onPreferenceChange(Preference preference, Object newValue) {
                            if (newValue.toString().equals("true")) {
                                Screen.screen_on(getActivity());
                                showSnackbar(getView());
                            } else {
                                Screen.screen_off(getActivity());
                                showSnackbar(getView());
                            }
                            return true;
                        }
                    });
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = super.onCreateView(inflater, container, savedInstanceState);

            view.setBackgroundColor(getResources().getColor(R.color.green_800));

            return view;
        }


        public static void showSnackbar(View view) {
            Snackbar snackbar = Snackbar
                    .make(view, "There is one text", Snackbar.LENGTH_LONG)
                    .setAction("Action Text", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Log.v("snackbar","yoo clicked action");
                        }
                    });

            // Changing message text color
            snackbar.setActionTextColor(Color.RED);

            // Changing action button text color
            View sbView = snackbar.getView();
            TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
            textView.setTextColor(Color.YELLOW);

            snackbar.show();
        }
    }

答案 1 :(得分:1)

我在我的Utils中有这个代码,你可以尝试:

public static void showSnackbar(final Activity activity) {
    Snackbar snackbar = Snackbar
    .make(activity.findViewById(android.R.id.content), "There is one text", Snackbar.LENGTH_LONG)
    .setAction("Action Text", new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Log.v("snackbar","yoo clicked action");
            }
          });

    // Changing message text color
    snackbar.setActionTextColor(Color.RED);

    // Changing action button text color
    View sbView = snackbar.getView();
    TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
    textView.setTextColor(Color.YELLOW);

    snackbar.show();
}

答案 2 :(得分:1)

根据文档 - https://developer.android.com/reference/android/support/design/widget/Snackbar.html#make(android.view.View,%20java.lang.CharSequence,%20int)

您将需要父视图,它将作为第一个参数传递给make()方法, 因此,在Fragment的布局文件中,找出父视图,即RelativeLayout或LinearLayout。使用view.findViewById()方法获取相同的XML引用。并将父视图的对象作为第一个参数传递。希望它会有所帮助!