gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:40:54 24 4
gpt4 key购买 nike

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

我的问题是我的应用程序总是在 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.

渐变构建:

// 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()
}
}

我希望,你能帮助我。

祝你有美好的一天:)

最佳答案

你应该像这样获得 Shared-Preference

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com