Android 使用 SharedPreferences.contains() 时应用崩溃
Android App crashes when using SharedPreferences.contains()
首先,如果我的英语不太正确,我想说对不起,因为我是德国人。还有一些 class/method/variable 名字是德国人。
我的问题是我的应用程序总是在 strtup 时崩溃。我已经发现(通过注释掉不同的东西),这是因为我在 onResume() 方法中的 if 条件,但我无法解决这个问题。
当应用程序完成时,它的意思是告诉我当天我必须带什么去学校。当然也会有一些额外的/很酷的功能。
我已经检查过,所有活动都有匹配的布局。
这是我的启动器代码 activity:
package de.mbauer.schulplaner;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class Main extends AppCompatActivity {
static SharedPreferences fachPrefs;
static SharedPreferences.Editor fachPrefEdit;
Intent keinFach;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
fachPrefs = this.getSharedPreferences("FaecherDatei", Context.MODE_PRIVATE);
fachPrefEdit = fachPrefs.edit();
if (!fachPrefs.contains("Fach0")) {
keinFach = new Intent(Main.this, KeinFachVorhanden.class);
startActivity(keinFach);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
fachPrefEdit.clear();
fachPrefEdit.commit();
}
}
这里是 Activity,由 onResume() 中的 Intent 调用:
package de.mbauer.schulplaner;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class KeinFachVorhanden extends AppCompatActivity implements View.OnClickListener {
TextView tv1;
Button btn1;
Intent faechererstellenOeffnen = new Intent(KeinFachVorhanden.this, FaecherErstellen.class );
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kein_fach_layout);
btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(this);
tv1 = (TextView) findViewById(R.id.textView);
}
@Override
public void onClick(View v) {
startActivity(faechererstellenOeffnen);
}
}
这是 xml 清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.mbauer.schulplaner">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FaecherErstellen" />
<activity android:name=".KeinFachVorhanden"></activity>
</application>
这里是 LogCat:
10-06 12:08:45.648 13621-13621/? I/art: Late-enabling -Xcheck:jni
10-06 12:08:45.747 13621-13621/de.mbauer.schulplaner W/System: ClassLoader referenced unknown path: /data/app/de.mbauer.schulplaner-2/lib/arm64
10-06 12:08:45.754 13621-13621/de.mbauer.schulplaner I/InstantRun: Instant Run Runtime started. Android package is de.mbauer.schulplaner, real application class is null.
10-06 12:08:45.972 13621-13621/de.mbauer.schulplaner W/System: ClassLoader referenced unknown path: /data/app/de.mbauer.schulplaner-2/lib/arm64
10-06 12:08:46.170 13621-13621/de.mbauer.schulplaner W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
10-06 12:08:46.409 13621-13621/de.mbauer.schulplaner I/HwSecImmHelper: mSecurityInputMethodService is null
10-06 12:08:46.419 13621-13621/de.mbauer.schulplaner E/HAL: load: id=gralloc != hmi->id=gralloc
10-06 12:08:46.506 13621-13621/de.mbauer.schulplaner I/Process: Sending signal. PID: 13621 SIG: 9
堆栈跟踪:
$ adb shell am start -n "de.mbauer.schulplaner/de.mbauer.schulplaner.Main" - a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 17199 on device huawei-ale_l21-QLF7N16307003297
W/System: ClassLoader referenced unknown path: /data/app/de.mbauer.schulplaner-1/lib/arm64
I/InstantRun: Instant Run Runtime started. Android package is de.mbauer.schulplaner, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/de.mbauer.schulplaner-1/lib/arm64
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android. graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package- private method in android.graphics.drawable.Drawable
I/HwSecImmHelper: mSecurityInputMethodService is null
E/HAL: load: id=gralloc != hmi->id=gralloc
E/HAL: load: id=gralloc != hmi->id=gralloc
I/OpenGLRenderer: Initialized EGL, version 1.4
W/OpenGLRenderer: load: so=/system/lib64/libhwuibp.so
dlopen failed: library "/system/lib64/libhwuibp.so" not found
W/OpenGLRenderer: Initialize Binary Program Cache: Load Failed
E/HAL: load: id=gralloc != hmi->id=gralloc
I/Process: Sending signal. PID: 17199 SIG: 9
Application terminated.
Gradle 构建:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
我希望,你能帮助我。
祝你有愉快的一天:)
你应该得到这样的共享首选项
SharedPreference pref = getApplicationContext().getSharedPreferences("CachedResponse", 0);
String hospitalJson = pref.getString("hospitalJson", null);
现在,如果 sharedpreference 中有 hospitalJson,它将 return 字符串,否则字符串将为空。然后你可以像下面的代码一样检查它是否为空:-
if (hospitalJson != null) {
//Do Something
}
首先,如果我的英语不太正确,我想说对不起,因为我是德国人。还有一些 class/method/variable 名字是德国人。
我的问题是我的应用程序总是在 strtup 时崩溃。我已经发现(通过注释掉不同的东西),这是因为我在 onResume() 方法中的 if 条件,但我无法解决这个问题。
当应用程序完成时,它的意思是告诉我当天我必须带什么去学校。当然也会有一些额外的/很酷的功能。
我已经检查过,所有活动都有匹配的布局。
这是我的启动器代码 activity:
package de.mbauer.schulplaner;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class Main extends AppCompatActivity {
static SharedPreferences fachPrefs;
static SharedPreferences.Editor fachPrefEdit;
Intent keinFach;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
fachPrefs = this.getSharedPreferences("FaecherDatei", Context.MODE_PRIVATE);
fachPrefEdit = fachPrefs.edit();
if (!fachPrefs.contains("Fach0")) {
keinFach = new Intent(Main.this, KeinFachVorhanden.class);
startActivity(keinFach);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
fachPrefEdit.clear();
fachPrefEdit.commit();
}
}
这里是 Activity,由 onResume() 中的 Intent 调用:
package de.mbauer.schulplaner;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class KeinFachVorhanden extends AppCompatActivity implements View.OnClickListener {
TextView tv1;
Button btn1;
Intent faechererstellenOeffnen = new Intent(KeinFachVorhanden.this, FaecherErstellen.class );
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kein_fach_layout);
btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(this);
tv1 = (TextView) findViewById(R.id.textView);
}
@Override
public void onClick(View v) {
startActivity(faechererstellenOeffnen);
}
}
这是 xml 清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.mbauer.schulplaner">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FaecherErstellen" />
<activity android:name=".KeinFachVorhanden"></activity>
</application>
这里是 LogCat:
10-06 12:08:45.648 13621-13621/? I/art: Late-enabling -Xcheck:jni
10-06 12:08:45.747 13621-13621/de.mbauer.schulplaner W/System: ClassLoader referenced unknown path: /data/app/de.mbauer.schulplaner-2/lib/arm64
10-06 12:08:45.754 13621-13621/de.mbauer.schulplaner I/InstantRun: Instant Run Runtime started. Android package is de.mbauer.schulplaner, real application class is null.
10-06 12:08:45.972 13621-13621/de.mbauer.schulplaner W/System: ClassLoader referenced unknown path: /data/app/de.mbauer.schulplaner-2/lib/arm64
10-06 12:08:46.170 13621-13621/de.mbauer.schulplaner W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
10-06 12:08:46.409 13621-13621/de.mbauer.schulplaner I/HwSecImmHelper: mSecurityInputMethodService is null
10-06 12:08:46.419 13621-13621/de.mbauer.schulplaner E/HAL: load: id=gralloc != hmi->id=gralloc
10-06 12:08:46.506 13621-13621/de.mbauer.schulplaner I/Process: Sending signal. PID: 13621 SIG: 9
堆栈跟踪:
$ adb shell am start -n "de.mbauer.schulplaner/de.mbauer.schulplaner.Main" - a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 17199 on device huawei-ale_l21-QLF7N16307003297
W/System: ClassLoader referenced unknown path: /data/app/de.mbauer.schulplaner-1/lib/arm64
I/InstantRun: Instant Run Runtime started. Android package is de.mbauer.schulplaner, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/de.mbauer.schulplaner-1/lib/arm64
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android. graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package- private method in android.graphics.drawable.Drawable
I/HwSecImmHelper: mSecurityInputMethodService is null
E/HAL: load: id=gralloc != hmi->id=gralloc
E/HAL: load: id=gralloc != hmi->id=gralloc
I/OpenGLRenderer: Initialized EGL, version 1.4
W/OpenGLRenderer: load: so=/system/lib64/libhwuibp.so
dlopen failed: library "/system/lib64/libhwuibp.so" not found
W/OpenGLRenderer: Initialize Binary Program Cache: Load Failed
E/HAL: load: id=gralloc != hmi->id=gralloc
I/Process: Sending signal. PID: 17199 SIG: 9
Application terminated.
Gradle 构建:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
我希望,你能帮助我。
祝你有愉快的一天:)
你应该得到这样的共享首选项
SharedPreference pref = getApplicationContext().getSharedPreferences("CachedResponse", 0);
String hospitalJson = pref.getString("hospitalJson", null);
现在,如果 sharedpreference 中有 hospitalJson,它将 return 字符串,否则字符串将为空。然后你可以像下面的代码一样检查它是否为空:-
if (hospitalJson != null) {
//Do Something
}