java - 使用 SharedPreferences.contains() 时 Android 应用程序崩溃

标签 java android sharedpreferences android-sharedpreferences

首先,如果我的英语不太正确,我想说抱歉,因为我是德国人。还有一些类/方法/变量名称是德语。

我的问题是我的应用程序总是在 strtup 时崩溃。我已经发现(通过注释掉不同的东西),这是因为 onResume() 方法中的 if 条件,但我无法解决这个问题。

应用程序完成后,它的意思是告诉我今天我必须带什么去学校。当然还会有一些额外/很酷的功能。

我已经检查过,所有 Activity 都有匹配的布局。

这是我的启动器 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 list :

<?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,它将返回字符串,否则字符串将为空。然后你可以检查它是否为空或不像下面的代码:-

if (hospitalJson != null) {
 //Do Something
}

关于java - 使用 SharedPreferences.contains() 时 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39894146/

相关文章:

java - 自定义 JComboBox 顶部标签

java - 为给定场景投票选出最佳协议(protocol)

java - 以编程方式更改首选项值后更新它们

android - 设备管理员弃用后锁定相机

android - 如何在重新打开应用程序后检索 editText 保存的值?

java - Java 和 C# 之间 double 精度的差异

带有基于计数的子列表的 Java 排序集合

java - RxJava : How to prepend startWith() default emit EVERY TIME parent observable emits?

java - 如何在 SharedPreferences 中存储和检索 Location 对象?

android - 我如何以编程方式在android中实现奖励系统?