编程语言
首页 > 编程语言> > java-在偏好片段中显示Snackbar

java-在偏好片段中显示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;
        }
    }

GradGrad相关性:

    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>

解决方法:

因此,在@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();
        }
    }

标签:sharedpreferences,android-view,android-snackbar,java,android
来源: https://codeday.me/bug/20191026/1940381.html