尝试打开片段时,显示此错误

时间:2018-09-26 18:06:39

标签: java android android-studio

我是android应用开发人员中的漂亮菜鸟。我正在尝试打开创建一个电子书应用程序。该应用程序正在运行。但是,当我尝试打开下载片段时,发生此错误并且应用程序崩溃了! 这是片段活动的Java代码-     包com.domain.app.Fragment;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.loopj.android.http.*;

import com.domain.app.Activity.MainActivity;
import com.domain.app.Activity.Search;
import com.domain.app.Adapter.DownloadAdapter;
import com.domain.app.DataBase.DatabaseHandler;
import com.domain.app.Item.DownloadList;
import com.domain.app.R;
import com.domain.app.Util.Constant_Api;
import com.wang.avi.AVLoadingIndicatorView;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;


public class DownloadFragment extends Fragment {

    private RecyclerView recyclerView;
    private AVLoadingIndicatorView progressBar;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private DownloadAdapter downloadAdapter;
    public MenuItem searchItem;
    private List<File> inFiles;
    private List<DownloadList> downloadListsCompair;
    private String root;
    private DatabaseHandler db;
    private ProgressDialog progressDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = LayoutInflater.from(getActivity()).inflate(R.layout.category_fragment, container, false);

        MainActivity.toolbar.setTitle(getResources().getString(R.string.download));

        db = new DatabaseHandler(getActivity());
        progressDialog = new ProgressDialog(getActivity());

        inFiles = new ArrayList<>();
        downloadListsCompair = new ArrayList<>();
        root = Environment.getExternalStorageDirectory() + "/appName/";

        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefresh_category_fragment);
        progressBar = (AVLoadingIndicatorView) view.findViewById(R.id.progressbar_category_fragment);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_category_fragment);

        progressBar.hide();

        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setFocusable(false);

        mSwipeRefreshLayout.setColorSchemeResources(R.color.toolbar);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Execute().execute();
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });

        new Execute().execute();

        setHasOptionsMenu(true);
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.search_menu, menu);

        searchItem = menu.findItem(R.id.ic_searchView);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setOnQueryTextListener((new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                startActivity(new Intent(getActivity(), Search.class)
                        .putExtra("search", query));
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return true;
            }
        }));

        MenuItemCompat.setOnActionExpandListener(searchItem,
                new MenuItemCompat.OnActionExpandListener() {
                    @Override
                    public boolean onMenuItemActionCollapse(MenuItem item) {
                        // Do something when collapsed
                        return true; // Return true to collapse action view
                    }

                    @Override
                    public boolean onMenuItemActionExpand(MenuItem item) {
                        // Do something when expanded
                        return true; // Return true to expand action view
                    }
                });

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // action with ID action_refresh was selected
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    class Execute extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            progressDialog.show();
            progressDialog.setMessage(getResources().getString(R.string.loading));
            progressDialog.setCancelable(false);
            Constant_Api.downloadLists.clear();
            inFiles.clear();
            downloadListsCompair.clear();
            Constant_Api.db = new DatabaseHandler(getContext());
            Constant_Api.downloadLists = Constant_Api.db.getDownload();
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... strings) {
            File file = new File(root);
            getListFiles(file);
            getDownloadLists(inFiles);
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            downloadAdapter = new DownloadAdapter(getActivity(), downloadListsCompair);
            recyclerView.setAdapter(downloadAdapter);
            progressDialog.dismiss();
            super.onPostExecute(s);
        }
    }

    private List<File> getListFiles(File parentDir) {
        // List<File> inFiles = new ArrayList<>();
        Queue<File> files = new LinkedList<>();
        files.addAll(Arrays.asList(parentDir.listFiles()));
        while (!files.isEmpty()) {
            File file = files.remove();
            if (file.isDirectory()) {
                files.addAll(Arrays.asList(file.listFiles()));
            } else if (file.getName().endsWith(".epub") || file.getName().endsWith(".pdf")) {
                inFiles.add(file);
            }
        }
        return inFiles;
    }

    private List<DownloadList> getDownloadLists(List<File> list) {
        for (int i = 0; i < Constant_Api.downloadLists.size(); i++) {
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).toString().contains(Constant_Api.downloadLists.get(i).getUrl())) {
                    downloadListsCompair.add(Constant_Api.downloadLists.get(i));
                    break;
                } else {
                    if (j == list.size() - 1) {
                        db.deletePdf(Constant_Api.downloadLists.get(i).getId());
                    }
                }
            }
        }
        return downloadListsCompair;
    }

}

这是我在Android Studio的“运行”窗口中看到的错误-

    V/FA: Inactivity, disconnecting from the service
I/zygote: JIT allocated 56KB for compiled code of void android.view.View.<init>(android.content.Context, android.util.AttributeSet, int, int)
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
                  Process: com.domain.app, PID: 12493
                  java.lang.RuntimeException: An error occurred while executing doInBackground()
                      at android.os.AsyncTask$3.done(AsyncTask.java:353)
                      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
                      at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
                      at java.util.concurrent.FutureTask.run(FutureTask.java:271)
                      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                      at java.lang.Thread.run(Thread.java:764)
                   Caused by: java.lang.NullPointerException
                      at java.util.Objects.requireNonNull(Objects.java:203)
                      at java.util.Arrays$ArrayList.<init>(Arrays.java:3741)
                      at java.util.Arrays.asList(Arrays.java:3728)
                      at com.domain.app.Fragment.DownloadFragment.getListFiles(DownloadFragment.java:181)
                      at com.domain.app.Fragment.DownloadFragment.access$500(DownloadFragment.java:43)
                      at com.domain.app.Fragment.DownloadFragment$Execute.doInBackground(DownloadFragment.java:164)
                      at com.domain.app.Fragment.DownloadFragment$Execute.doInBackground(DownloadFragment.java:146)
                      at android.os.AsyncTask$2.call(AsyncTask.java:333)
                      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
                      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
                      at java.lang.Thread.run(Thread.java:764) 
E/RecyclerView: No adapter attached; skipping layout
V/FA: Recording user engagement, ms: 10252
V/FA: Connecting to remote service
V/FA: Activity paused, time: 4589154
D/FA: Logging event (FE): adunit_exposure(_xu), Bundle[{firebase_event_origin(_o)=am, ad_unit_id(_ai)=ca-app-pub-3940256099942544/6300978111, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=751889283038278768, exposure_time(_xt)=9442}]
V/FA: Connection attempt already in progress
D/FA: Logging event (FE): ad_exposure(_xa), Bundle[{firebase_event_origin(_o)=am, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=751889283038278768, exposure_time(_xt)=9442}]
V/FA: Connection attempt already in progress
D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=10252, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=751889283038278768}]
V/FA: Connection attempt already in progress
      Not logging ad unit exposure. No active activity
      Not logging ad exposure. No active activity
D/EGL_emulation: eglMakeCurrent: 0xe62e4f20: ver 2 0 (tinfo 0xe624c000)
D/EGL_emulation: eglMakeCurrent: 0xe62e4f20: ver 2 0 (tinfo 0xe624c000)
D/EGL_emulation: eglMakeCurrent: 0xe62e4f20: ver 2 0 (tinfo 0xe624c000)
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 4
D/EGL_emulation: eglMakeCurrent: 0xe62e4f20: ver 2 0 (tinfo 0xe624c000)
D/EGL_emulation: eglMakeCurrent: 0xe62e4f20: ver 2 0 (tinfo 0xe624c000)
D/EGL_emulation: eglMakeCurrent: 0xe62e4f20: ver 2 0 (tinfo 0xe624c000)
E/WindowManager: android.view.WindowLeaked: Activity com.domain.app.Activity.MainActivity has leaked window DecorView@2ec76c[] that was originally added here
                     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:485)
                     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:346)
                     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
                     at android.app.Dialog.show(Dialog.java:330)
                     at com.domain.app.Fragment.DownloadFragment$Execute.onPreExecute(DownloadFragment.java:150)
                     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:648)
                     at android.os.AsyncTask.execute(AsyncTask.java:595)
                     at com.domain.app.Fragment.DownloadFragment.onCreateView(DownloadFragment.java:91)
                     at android.support.v4.app.Fragment.performCreateView(Fragment.java:2343)
                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1421)
                     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1752)
                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1821)
                     at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
                     at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2595)
                     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2382)
                     at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2337)
                     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2244)
                     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:702)
                     at android.os.Handler.handleCallback(Handler.java:790)
                     at android.os.Handler.dispatchMessage(Handler.java:99)
                     at android.os.Looper.loop(Looper.java:164)
                     at android.app.ActivityThread.main(ActivityThread.java:6494)
                     at java.lang.reflect.Method.invoke(Native Method)
                     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
V/FA: Inactivity, disconnecting from the service
I/Ads: Ad is not visible. Not refreshing ad.
       Scheduling ad refresh 60000 milliseconds from now.
I/Ads: Ad is not visible. Not refreshing ad.
       Scheduling ad refresh 60000 milliseconds from now.

请帮助我找到实际的问题以及该错误的解决方案!预先感谢...:)

0 个答案:

没有答案