如何修复致命异常:AsyncTask#1

时间:2019-01-22 09:37:18

标签: android android-asynctask

我正在学习如何在天气预报中按预期运行所有天气预报,因为在代码的每一行中都没有发现任何错误,但是当我尝试执行代码时,我的应用程序强制关闭

错误看起来像这样:

01-22 16:14:45.114 11085-11085/? I/art: Late-enabling -Xcheck:jni
01-22 16:14:45.136 11085-11085/? D/TidaProvider: TidaProvider()
01-22 16:14:45.159 11085-11085/? W/ActivityThread: Application info.landingcasts.myapplication is waiting for the debugger on port 8100...
01-22 16:14:45.159 11085-11085/? I/System.out: Sending WAIT chunk
01-22 16:14:45.338 11085-11091/info.landingcasts.myapplication I/art: Debugger is active
01-22 16:14:45.360 11085-11085/info.landingcasts.myapplication I/System.out: Debugger has connected
01-22 16:14:45.360 11085-11085/info.landingcasts.myapplication I/System.out: waiting for debugger to settle...
01-22 16:14:45.560 11085-11085/info.landingcasts.myapplication I/System.out: waiting for debugger to settle...
01-22 16:14:45.760 11085-11085/info.landingcasts.myapplication I/System.out: waiting for debugger to settle...
01-22 16:14:45.961 11085-11085/info.landingcasts.myapplication I/System.out: waiting for debugger to settle...
01-22 16:14:46.161 11085-11085/info.landingcasts.myapplication I/System.out: waiting for debugger to settle...
01-22 16:14:46.361 11085-11085/info.landingcasts.myapplication I/System.out: waiting for debugger to settle...
01-22 16:14:46.562 11085-11085/info.landingcasts.myapplication I/System.out: waiting for debugger to settle...
01-22 16:14:46.762 11085-11085/info.landingcasts.myapplication I/System.out: waiting for debugger to settle...
01-22 16:14:46.963 11085-11085/info.landingcasts.myapplication I/System.out: debugger has settled (1457)
01-22 16:14:46.969 11085-11085/info.landingcasts.myapplication W/System: ClassLoader referenced unknown path: /data/app/info.landingcasts.myapplication-2/lib/arm64
01-22 16:14:46.994 11085-11085/info.landingcasts.myapplication 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
01-22 16:14:47.027 11085-11085/info.landingcasts.myapplication V/BoostFramework: mAcquireFunc method = public int com.qualcomm.qti.Performance.perfLockAcquire(int,int[])
01-22 16:14:47.028 11085-11085/info.landingcasts.myapplication V/BoostFramework: mReleaseFunc method = public int com.qualcomm.qti.Performance.perfLockRelease()
01-22 16:14:47.028 11085-11085/info.landingcasts.myapplication V/BoostFramework: mAcquireTouchFunc method = public int com.qualcomm.qti.Performance.perfLockAcquireTouch(android.view.MotionEvent,android.util.DisplayMetrics,int,int[])
01-22 16:14:47.028 11085-11085/info.landingcasts.myapplication V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String)
01-22 16:14:47.028 11085-11085/info.landingcasts.myapplication V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop()
01-22 16:14:47.031 11085-11085/info.landingcasts.myapplication V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@b391908
01-22 16:14:47.031 11085-11085/info.landingcasts.myapplication V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@12048a1
01-22 16:14:47.037 11085-11085/info.landingcasts.myapplication I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
01-22 16:14:47.037 11085-11085/info.landingcasts.myapplication I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
01-22 16:14:47.108 11085-11102/info.landingcasts.myapplication I/System.out: filename : city.list.json
01-22 16:14:47.109 11085-11102/info.landingcasts.myapplication W/System.err: java.io.FileNotFoundException: city.list.json
01-22 16:14:47.110 11085-11102/info.landingcasts.myapplication W/System.err:     at android.content.res.AssetManager.openAsset(Native Method)
01-22 16:14:47.110 11085-11102/info.landingcasts.myapplication W/System.err:     at android.content.res.AssetManager.open(AssetManager.java:317)
01-22 16:14:47.110 11085-11102/info.landingcasts.myapplication W/System.err:     at info.landingcasts.myapplication.helpers.CustomApplication.getJsonStream(CustomApplication.java:29)
01-22 16:14:47.110 11085-11102/info.landingcasts.myapplication W/System.err:     at info.landingcasts.myapplication.MainActivity$PrepareDataSource.doInBackground(MainActivity.java:72)
01-22 16:14:47.110 11085-11102/info.landingcasts.myapplication W/System.err:     at info.landingcasts.myapplication.MainActivity$PrepareDataSource.doInBackground(MainActivity.java:62)
01-22 16:14:47.111 11085-11102/info.landingcasts.myapplication W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
01-22 16:14:47.111 11085-11102/info.landingcasts.myapplication W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-22 16:14:47.111 11085-11102/info.landingcasts.myapplication W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
01-22 16:14:47.111 11085-11102/info.landingcasts.myapplication W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
01-22 16:14:47.111 11085-11102/info.landingcasts.myapplication W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
01-22 16:14:47.111 11085-11102/info.landingcasts.myapplication W/System.err:     at java.lang.Thread.run(Thread.java:818)
01-22 16:14:47.116 11085-11102/info.landingcasts.myapplication E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: info.landingcasts.myapplication, PID: 11085
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:309)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.NullPointerException: lock == null
        at java.io.Reader.<init>(Reader.java:64)
        at java.io.InputStreamReader.<init>(InputStreamReader.java:77)
        at info.landingcasts.myapplication.helpers.CustomApplication.readStream(CustomApplication.java:40)
        at info.landingcasts.myapplication.MainActivity$PrepareDataSource.doInBackground(MainActivity.java:73)
        at info.landingcasts.myapplication.MainActivity$PrepareDataSource.doInBackground(MainActivity.java:62)
        at android.os.AsyncTask$2.call(AsyncTask.java:295)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
        at java.lang.Thread.run(Thread.java:818) 
01-22 16:14:47.142 11085-11105/info.landingcasts.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-22 16:14:47.187 11085-11085/info.landingcasts.myapplication D/ActivityThreadInjector: clearCachedDrawables.
01-22 16:14:47.249 11085-11105/info.landingcasts.myapplication I/Adreno: QUALCOMM build                   : 3689ef7, Ibb8f3faafb
    Build Date                       : 07/15/16
    OpenGL ES Shader Compiler Version: XE031.08.00.00
    Local Branch                     : 
    Remote Branch                    : 
    Remote Branch                    : 
    Reconstruct Branch               : 
01-22 16:14:47.269 11085-11105/info.landingcasts.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
01-22 16:14:51.492 11085-11102/info.landingcasts.myapplication I/Process: Sending signal. PID: 11085 SIG: 9

这是我的代码类:

  1. CustomApplication类

import android.app.Application;
import android.content.res.AssetManager;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonReader;
import info.landingcasts.myapplication.entity.ListJsonObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CustomApplication extends Application {
    public InputStream getJsonStream(){
        AssetManager mgr = getAssets();
        String filename = null;
        InputStream stream = null;

        try {
            filename = "city.list.json";
            System.out.println("filename : " + filename);
            stream = mgr.open(filename, AssetManager.ACCESS_BUFFER);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stream;
    }

    public List<ListJsonObject> readStream(InputStream stream) {
        JsonReader reader = null;
        List<ListJsonObject> messages = new ArrayList<ListJsonObject>();
        try {
            reader = new JsonReader(new InputStreamReader(stream, "UTF-8"));
            Gson gson = new GsonBuilder().create();
            reader.beginArray();
            while (reader.hasNext()) {
                ListJsonObject message = gson.fromJson(reader, ListJsonObject.class);
                messages.add(message);
            }
            reader.endArray();
            reader.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }catch (IOException ex) {
        }
        Collections.sort(messages, new Comparator<ListJsonObject>() {
            @Override
            public int compare(ListJsonObject listJsonObject, ListJsonObject nextListJsonObject) {
                return listJsonObject.getName().compareToIgnoreCase(nextListJsonObject.getName());
            }
        });
        return messages;
    }
}

  1. MainActivity类

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;

import com.google.common.collect.Lists;
import com.google.common.math.IntMath;
import info.landingcasts.myapplication.entity.ListJsonObject;
import info.landingcasts.myapplication.helpers.CustomApplication;
import info.landingcasts.myapplication.helpers.CustomSharedPreference;
import info.landingcasts.myapplication.helpers.Helper;

import java.io.InputStream;
import java.math.RoundingMode;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    private final int SPLASH_DISPLAY_LENGTH = 5000;

    private CustomSharedPreference customSharedPreference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.hide();
        }

        customSharedPreference = new CustomSharedPreference(MainActivity.this);
        if(!customSharedPreference.getDataSourceIfPresent()){
            PrepareDataSource mDataSource = new PrepareDataSource();
            mDataSource.execute();
        }

        new Handler().postDelayed(new Runnable(){
            @Override
            public void run(){
                Intent startActivityIntent = new Intent(MainActivity.this, WeatherActivity.class);
                startActivity(startActivityIntent);
                MainActivity.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }

    private class PrepareDataSource extends AsyncTask<Void, Void, Void> {

        protected void onProgressUpdate() {
        }

        protected void onPostExecute() {
        }

        @Override
        protected Void doInBackground(Void... voids) {
            InputStream stream = ((CustomApplication)getApplication()).getJsonStream();
            List<ListJsonObject> storeSourceData = ((CustomApplication)getApplication()).readStream(stream);
            // store data in shared reference
            int partitionSize = IntMath.divide(storeSourceData.size(), 2, RoundingMode.UP);
            List<List<ListJsonObject>> partitions = Lists.partition(storeSourceData, partitionSize);
            List<ListJsonObject> firstListObject = partitions.get(0);
            List<ListJsonObject> secondListObject = partitions.get(1);

            customSharedPreference.setDataFromSharedPreferences(Helper.STORED_DATA_FIRST, firstListObject);
            customSharedPreference.setDataFromSharedPreferences(Helper.STORED_DATA_SECOND, secondListObject);
            customSharedPreference.setDataSourceIfPresent(true);

            return null;
        }
    }


}

答案是:是的,我忘了包括资产

0 个答案:

没有答案