SharedPreferences在Studio中崩溃我的应用程序
我想使用SharedPreferences来保存设置,但它会崩溃我的应用程序! 我曾在Eclipse中使用过它。
SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);
String font =prefs.getString("font", "");
int size =prefs.getInt("size", 10);
int space =prefs.getInt("space", 7);
final TextView txt = (TextView) findViewById(R.id.textView5);
SeekBar seeksize = (SeekBar) findViewById(R.id.seekBar1);
SeekBar fasele = (SeekBar) findViewById(R.id.seekBar2);
seeksize.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
//txt1.setTextSize(arg1);
txt.setTextSize(arg1);
}
});
fasele.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
//txt1.setTextSize(arg1);
txt.setLineSpacing(arg1,1);
}
});
final Typeface zar = Typeface.createFromAsset(getAssets(), "font/BZAR.TTF");
final Typeface nazanin = Typeface.createFromAsset(getAssets(), "font/BNAZANIN.TTF");
final Typeface kodak = Typeface.createFromAsset(getAssets(), "font/BKOODKBD.TTF");
RadioButton rb_nazanin = (RadioButton) findViewById(R.id.radio0);
RadioButton rb_zar = (RadioButton) findViewById(R.id.radio1);
RadioButton rb_kodak = (RadioButton) findViewById(R.id.radio2);
rb_nazanin.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@SuppressLint("NewApi")
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (arg1 == true)
{
txt.setTypeface(nazanin);
SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putString("font", "nazanin");
editor.apply();
}
}
});
rb_zar.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (arg1 == true)
{
txt.setTypeface(zar);
SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putString("font", "zar");
editor.apply();
}
}
});
rb_kodak.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (arg1 == true)
{
txt.setTypeface(kodak);
SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putString("font", "kodak");
editor.apply();
}
}
});
Logcat错误
03-17 02:24:46.113 1700-1700 / com.example.material.testnavi E / AndroidRuntime:致命异常:main java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.material.testnavi / com.example .material.testnavi.MainTan}:了java.lang.RuntimeException:天然字体不能在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)制成在android.app.ActivityThread.access $ 600(ActivityThread.java:141)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)在android.os.Handler.dispatchMessage(Handler.java:99)的机器人。 os.Looper.loop(Looper.java:137)at android.app.ActivityThread.main(ActivityThread.java:5041)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method。调用(Method.java:511)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560 )在dalvik.system.NativeStart.main(Native方法)引起的:java.lang.RuntimeException:原生字体不能在android.graphics.Typeface。(Typeface.java:175)at android.graphics.Typeface.createFromAsset( Typeface.java:149)在com.example.material.testnavi.MainTan.onCreate(MainTan.java:79)在android.app.Activity.performCreate(Activity.java:5104)在android.app.Instrumentation.callActivityOnCreate(仪表的.java:1080)在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)在android.app.ActivityThread.access $ 600(ActivityThread.java:141 )at android.app.ActivityThread $ h.handleMessage(ActivityThread.java:1234)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:137)at android .app.ActivityThread.main(ActivityThread.java:5041)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:511)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)在dalvik.system.NativeStart.main(本机方法)
我想我可能会假设你的错误是什么(没有日志,我可能是错的),但是你从defaultSharedPrefs获得值:
SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);
并将它们设置为另一个:
SharedPreferences shared = getSharedPreferences("Prefs", MODE_PRIVATE);
这可能是为什么你检索的值都不好。 将这最后一行替换为:
SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);
使用相同的首选项获取和设置值。
链接地址: http://www.djcxy.com/p/31437.html上一篇: SharedPreferences crashes my app in Studio
下一篇: Null pointer exception when loading items to the spinner