AsyncTask中的SplashScreen与SharedPreferences

时间:2017-05-13 18:49:40

标签: android android-asynctask sharedpreferences splash-screen

我想将加载屏幕设为SplashScreen,而应用程序使用SharedPrferences的AsyncTask加载数据。我需要它来改变应用程序的主题(和后来的语言)。这是诅咒代码:

LogoActivity:

package com.ivan.vts.mapper.extended;

import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.ivan.vts.mapper.R;
import com.ivan.vts.mapper.map.MapsActivity;
import com.ivan.vts.mapper.settings.helper.MenuSwitcher;

public class LogoActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    new SplashScreen().execute();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_logo);
}

    private class SplashScreen extends AsyncTask<Void, Void, Integer> {

    @Override
    protected Integer doInBackground(Void... params) {
        SharedPreferences preferences;
        preferences = getSharedPreferences(Constants.APP_NAME, MODE_PRIVATE);
        int themeNo = preferences.getInt("primaryTheme", 0);
        return themeNo;
    }

    @Override
    protected void onPostExecute(Integer theme) {
        Bundle bundle = new Bundle();
        bundle.putInt("theme", theme);
        MenuSwitcher.getInstance().switchActivity(LogoActivity.this, MapsActivity.class, bundle);
    }
    }
}

的Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.ivan.vts.mapper">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application
    android:name="android.support.multidex.MultiDexApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    tools:replace="android:icon">
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name=".map.MapsActivity"
        android:configChanges="orientation|screenSize"
        android:label="@string/app_name"
        android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".navigation.NavigationActivity"
        android:configChanges="orientation|screenSize"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.SEND" />
        </intent-filter>
    </activity>
    <activity
        android:name=".settings.SettingActivity"
        android:configChanges="orientation|screenSize"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>
    </activity>
    <activity
        android:name=".history.HistoryActivity"
        android:configChanges="orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>
    </activity>
    <activity android:name=".extended.LogoActivity"
        android:configChanges="orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

gradle.built:

apply plugin: 'com.android.application'

android {     compileSdkVersion 24     buildToolsVersion“25.0.2”

defaultConfig {
    applicationId "com.ivan.vts.mapper"
    minSdkVersion 16
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
    jackOptions {
        enabled true
    }

}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    targetCompatibility JavaVersion.VERSION_1_8
    sourceCompatibility JavaVersion.VERSION_1_8
}

dexOptions {
    javaMaxHeapSize "2048M"
}

}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile group: 'com.google.code.gson', name: 'gson', version: '2.4'
compile group: 'commons-io', name: 'commons-io', version: '2.5'
compile 'com.android.volley:volley:1.0.0'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.google.android.gms:play-services-location:10.2.4'
compile 'com.google.android.gms:play-services-maps:10.2.4'
compile 'com.google.android.gms:play-services-places:10.2.4'
compile 'com.android.support:multidex:1.0.1'
compile 'cn.pedant.sweetalert:library:1.3'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.maps.android:android-maps-utils:0.4.+'
testCompile 'junit:junit:4.12'
}

activity_logo.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ivan.vts.mapper.extended.LogoActivity">
<ImageView
    android:id="@+id/imageView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:srcCompat="@drawable/file_tree"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp" />
</android.support.constraint.ConstraintLayout>

问题是这给了我一个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ivan.vts.mapper/com.ivan.vts.mapper.extended.LogoActivity}: android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020013
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
                                                                     at android.app.ActivityThread.access$600(ActivityThread.java:138)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                     at android.os.Looper.loop(Looper.java:213)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:4787)
                                                                     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:789)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
                                                                     at dalvik.system.NativeStart.main(Native Method)
                                                                  Caused by: android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020013
                                                                     at android.content.res.Resources.loadDrawable(Resources.java:1923)
                                                                     at android.content.res.Resources.getDrawable(Resources.java:664)
                                                                     at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:354)
                                                                     at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
                                                                     at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181)
                                                                     at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:689)
                                                                     at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
                                                                     at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77)
                                                                     at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:83)
                                                                     at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
                                                                     at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
                                                                     at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
                                                                     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193)
                                                                     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173)
                                                                     at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511)
                                                                     at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71)
                                                                     at com.ivan.vts.mapper.extended.LogoActivity.onCreate(LogoActivity.java:17)
                                                                     at android.app.Activity.performCreate(Activity.java:5008)
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096) 
                                                                     at android.app.ActivityThread.access$600(ActivityThread.java:138) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                     at android.os.Looper.loop(Looper.java:213) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:4787) 
                                                                     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:789) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
                                                                     at dalvik.system.NativeStart.main(Native Method) 
                                                                  Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: invalid drawable tag vector
                                                                     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:886)
                                                                     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822)
                                                                     at android.content.res.Resources.loadDrawable(Resources.java:1920)
                                                                     at android.content.res.Resources.getDrawable(Resources.java:664) 
                                                                     at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:354) 
                                                                     at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193) 
                                                                     at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181) 
                                                                     at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:689) 
                                                                     at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186) 
                                                                     at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:77) 
                                                                     at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:83) 
                                                                     at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146) 
                                                                     at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28) 
                                                                     at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41) 
                                                                     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193) 
                                                                     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173) 
                                                                     at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511) 
                                                                     at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:71) 
                                                                     at com.ivan.vts.mapper.extended.LogoActivity.onCreate(LogoActivity.java:17) 
                                                                     at android.app.Activity.performCreate(Activity.java:5008) 
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035) 
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096) 
                                                                     at android.app.ActivityThread.access$600(ActivityThread.java:138) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                     at android.os.Looper.loop(Looper.java:213) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:4787) 
                                                                     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:789) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
                                                                     at dalvik.system.NativeStart.main(Native Method) e

我的问题是如何做到这一点?我错过了什么?如果有人需要更多细节,我会编辑。提前谢谢!

0 个答案:

没有答案