我想使用EasyPreferences库但出现此错误:请在Application类onCreate中调用Builder.setContext()。build()

时间:2017-04-22 17:02:24

标签: android android-sharedpreferences

我想使用EasyPreferences库:https://github.com/Pixplicity/EasyPreferences

但出现此错误:请在Application类onCreate中调用Builder.setContext()。build()

PrefsApplication.java

  package ir.korandco.irkhoone_co_app.utility;

import android.app.Application;
import android.content.ContextWrapper;

import com.pixplicity.easyprefs.library.Prefs;

public class PrefsApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // Initialize the Prefs class
        new Prefs.Builder()
                .setContext(this)
                .setMode(ContextWrapper.MODE_PRIVATE)
                .setPrefsName(getPackageName())
                .setUseDefaultSharedPreference(true)
                .build();
    }

}

MainActivity.java

 package ir.korandco.irkhoone_co_app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.Indicators.PagerIndicator;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import com.pixplicity.easyprefs.library.Prefs;

import java.util.HashMap;

import ir.korandco.irkhoone_co_app.R;

public class MainActivity extends ActionBarActivity implements BaseSliderView.OnSliderClickListener, ViewPagerEx.OnPageChangeListener{

    private SliderLayout mDemoSlider;
    private String t;
    public static final String SAVED_TOKEN = "token";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        LayoutInflaterCompat.setFactory(getLayoutInflater(), new IconicsLayoutInflater(getDelegate()));
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        t = Prefs.getString(SAVED_TOKEN,"unavailable");

        mDemoSlider = (SliderLayout)findViewById(R.id.slider);

        HashMap<String,String> url_maps = new HashMap<String, String>();
        url_maps.put("Hannibal", "http://static2.hypable.com/wp-content/uploads/2013/12/hannibal-season-2-release-date.jpg");
        url_maps.put("Big Bang Theory", "http://tvfiles.alphacoders.com/100/hdclearart-10.png");
        url_maps.put("House of Cards", "http://cdn3.nflximg.net/images/3093/2043093.jpg");
        url_maps.put("Game of Thrones", "http://images.boomsbeat.com/data/images/full/19640/game-of-thrones-season-4-jpg.jpg");

        HashMap<String,Integer> file_maps = new HashMap<String, Integer>();
        file_maps.put("Hannibal",R.drawable.hannibal);
        file_maps.put("Big Bang Theory",R.drawable.bigbang);
        file_maps.put("House of Cards",R.drawable.house);
        file_maps.put("Game of Thrones", R.drawable.game_of_thrones);

        for(String name : file_maps.keySet()){
            TextSliderView textSliderView = new TextSliderView(this);
            // initialize a SliderLayout
            textSliderView
                    .description(name)
                    .image(file_maps.get(name))
                    .setScaleType(BaseSliderView.ScaleType.Fit)
                    .setOnSliderClickListener(this);

            //add your extra information
            textSliderView.bundle(new Bundle());
            textSliderView.getBundle()
                    .putString("extra",name);

            mDemoSlider.addSlider(textSliderView);
        }
        mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
        mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        mDemoSlider.setCustomAnimation(new DescriptionAnimation());
        mDemoSlider.setDuration(4000);
        mDemoSlider.addOnPageChangeListener(this);

        Toast.makeText(MainActivity.this, "token  : " + t, Toast.LENGTH_SHORT).show();

        Log.d("onResponse token :", t);

        //رفتن به صفحه عضویت آژانس املاک
        Button create_aj = (Button)findViewById(R.id.create_aj_btn);
        create_aj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,CreateAjencyActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });

        //رفتن به صفحه عضویت شرکت ها
        Button create_co = (Button)findViewById(R.id.create_co_btn);
        create_co.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!("unavailable".equals(t))){
                    Intent intent = new Intent(MainActivity.this,PanelActivity.class);
                    MainActivity.this.startActivity(intent);
                }else{
                    Intent intent = new Intent(MainActivity.this,SignupActivity.class);
                    MainActivity.this.startActivity(intent);
                }
            }
        });

        //رفتن به صفحه درباره ما
        Button about = (Button)findViewById(R.id.about_btn);
        about.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,AboutActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });

        //رفتن به صفحه تماس با ما
        Button contact = (Button)findViewById(R.id.contact_btn);
        contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,ContactActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });

        //رفتن به صفحه ورود به پنل کاربری
        Button login = (Button)findViewById(R.id.login_btn);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (!token.isEmpty()){
//                    Intent intent = new Intent(MainActivity.this,PanelActivity.class);
//                    MainActivity.this.startActivity(intent);
//                }else{
                    Intent intent = new Intent(MainActivity.this,LoginActivity.class);
                    MainActivity.this.startActivity(intent);
//                }
            }
        });

        //رفتن به صفحه معرفی پنل ها
        Button panel = (Button)findViewById(R.id.panle_btn);
        panel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,PanelActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });



        ImageButton main_menu_ic = (ImageButton) findViewById(R.id.main_menu_ic);
        main_menu_ic.setVisibility(View.GONE);

    }

    @Override
    protected void onStop() {
        // To prevent a memory leak on rotation, make sure to call stopAutoCycle() on the slider before activity or fragment is destroyed
        mDemoSlider.stopAutoCycle();
        super.onStop();
    }

    @Override
    public void onSliderClick(BaseSliderView slider) {
        Toast.makeText(this,slider.getBundle().get("extra") + "",Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_custom_indicator:
                mDemoSlider.setCustomIndicator((PagerIndicator) findViewById(R.id.custom_indicator));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    @Override
    public void onPageSelected(int position) {
        Log.d("Slider Demo", "Page Changed: " + position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {}

}

t = Prefs.getString(SAVED_TOKEN,&#34;不可用&#34;);

但遇到了这个错误:

请在Application类onCreate

中调用Builder.setContext()。build()

1 个答案:

答案 0 :(得分:5)

最有可能的是,您还没有在AndroidManifest.xml中定义您的应用程序类

<application
    android:name="ir.korandco.irkhoone_co_app.utility.PrefsApplication"
    ...
    >