所以我尝试在我的应用中使用this Intro Slider
一切都很好,直到我试图让它出现在第一次开始。
我为此使用了 sharedPreferences 。
这是我的IntroActivity:
public class MainIntroActivity extends com.heinrichreimersoftware.materialintro.app.IntroActivity {
private PrefManager prefManager;
@Override protected void onCreate(Bundle savedInstanceState) {
prefManager = new PrefManager(this);
if (!prefManager.isFirstTimeLaunch()) {
launchHomeScreen();
finish();
}
super.onCreate(savedInstanceState);
addSlide(new SimpleSlide.Builder()
.title(R.string.intro_title1)
.description(R.string.intro_description1)
.image(R.drawable.tired)
.background(R.color.colorPrimaryLight)
.backgroundDark(R.color.colorPrimaryDark)
.build());
}
private void launchHomeScreen() {
prefManager.setFirstTimeLaunch(false);
startActivity(new Intent(MainIntroActivity.this, MainActivity.class));
finish();
}
}
这是我的PrefManager类:
public class PrefManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
// shared pref mode
int PRIVATE_MODE = 0;
// Shared preferences file name
private static final String PREF_NAME = "MainIntroActivity";
private static final String IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch";
public PrefManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void setFirstTimeLaunch(boolean isFirstTime) {
editor.putBoolean(IS_FIRST_TIME_LAUNCH, isFirstTime);
editor.commit();
}
public boolean isFirstTimeLaunch() {
return pref.getBoolean(IS_FIRST_TIME_LAUNCH, true);
}
}
logcat:
Caused by: java.lang.NullPointerException
at com.heinrichreimersoftware.materialintro.app.IntroActivity.findViews(IntroActivity.java:282)
at com.heinrichreimersoftware.materialintro.app.IntroActivity.onCreate(IntroActivity.java:174)
at ege.ege.MainIntroActivity.onCreate(MainIntroActivity.java:21)
答案 0 :(得分:0)
在super.onCreate(savedInstanceState);
prefManager = new PrefManager(this);