package com.example.framework.untils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class SPUtils
{
private static volatile SPUtils mInstance;
private SharedPreferences.Editor editor;
private SharedPreferences sp;
public static SPUtils getInstance()
{
if (mInstance == null)
try
{
if (mInstance == null)
mInstance = new SPUtils();
}
finally
{
}
return mInstance;
}
public void deleteKey(String paramString)
{
this.editor.remove(paramString);
this.editor.commit();
}
public boolean getBoolean(String key, boolean defValue)
{
return this.sp.getBoolean(key, true);
}
public int getInt(String paramString, int paramInt)
{
return this.sp.getInt(paramString, paramInt);
}
public String getString(String key, String defValue)
{
return this.sp.getString(key, defValue);
}
public void initSp(Context paramContext)
{
this.sp = paramContext.getSharedPreferences("Config", 0);
this.editor = this.sp.edit();
}
public void putBoolean(String key, boolean Value)
{
this.editor.putBoolean(key, Value);
this.editor.commit();
}
public void putInt(String paramString, int paramInt)
{
this.editor.putInt(paramString, paramInt);
this.editor.commit();
}
public void putString(String paramString1, String paramString2)
{
this.editor.putString(paramString1, paramString2);
this.editor.commit();
}
}
package com.example.ido.ui;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import com.example.framework.entity.Constants;
import com.example.framework.untils.LogUntis;
import com.example.framework.untils.SPUtils;
import com.example.ido.MainActivity;
import com.example.ido.R;
import static com.example.framework.untils.LogUntis.*;
/**
*启动页
* 全屏
*
*/
public class IndexActivity extends AppCompatActivity {
private static final int SKIP_MAIN = 1000;
private Handler mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
switch (message.what){
case SKIP_MAIN:
LogUntis.e("0");
startMain();
}
return false;
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
mHandler.sendEmptyMessageDelayed(SKIP_MAIN,2*1000);
}
private void startMain() {
boolean isFirstApp = SPUtils.getInstance().getBoolean(Constants.SP_IS_FIRST_APP,true);
//boolean isFirstApp = true;
LogUntis.e("1");
Intent intent = new Intent();
if(isFirstApp){
//跳转引导页
LogUntis.e("2");
intent.setClass(this,GuideActivity.class);
SPUtils.getInstance().putBoolean(Constants.SP_IS_FIRST_APP,false);
}else{
String token = SPUtils.getInstance().getString(Constants.SP_TOKEN,"");
if (TextUtils.isEmpty(token)){
intent.setClass(this,LoginActivity.class);
}else {
intent.setClass(this, MainActivity.class);
}
}
startActivity(intent);
finish();
}
}
启动页跳转的时候定义的变量获取不到 报错空对象引用
Attempt to invoke virtual method 'boolean com.example.framework.untils.SPUtils.getBoolean(java.lang.String, boolean)' on a null object reference
看看这个 https://blog.csdn.net/ouyang_peng/article/details/12784303