App crashes on Android 6.0 launch, Logcat included

2019-09-12 14:41发布

问题:

So I have very little experience with Java but I can learn it quickly. When launching the app in Marshmallow 6.0 . It suddenly crashes. I can get this from logcat in the Android Studio

SKIP THE STRIKED TEXT AS ITS BEEN CORRECTED

E/AndroidRuntime: FATAL EXCEPTION: main E/AndroidRuntime: Process: nezibo.com.dreamception, PID: 1863 E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{nezibo.com.dreamception/nezibo.com.dreamception.activity.MainActivity}: java.lang.NullPointerException: Attempt to get length of null array E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java) E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148) E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417) E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to get length of null array E/AndroidRuntime: at nezibo.com.dreamception.utils.FileUtils.removeTMPFiles(FileUtils.java:31) E/AndroidRuntime: at nezibo.com.dreamception.activity.MainActivity.init(MainActivity.java:186) E/AndroidRuntime: at nezibo.com.dreamception.activity.MainActivity.onCreate(MainActivity.java:64) E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6237) E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)  E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)  E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)  E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)  E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)  E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

CRASHLYTICS SHOWS THIS

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{nezibo.com.dreamception/nezibo.com.dreamception.activity.MainActivity}: java.lang.NullPointerException: Attempt to get length of null array
   at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
   at android.app.ActivityThread.-wrap11(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
   at android.os.Handler.dispatchMessage(Handler.java)
   at android.os.Looper.loop(Looper.java)
   at android.app.ActivityThread.main(ActivityThread.java)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
Caused by java.lang.NullPointerException: Attempt to get length of null array
   at nezibo.com.dreamception.utils.FileUtils.removeTMPFiles(FileUtils.java)
   at nezibo.com.dreamception.activity.MainActivity.init(MainActivity.java)
   at nezibo.com.dreamception.activity.MainActivity.onCreate(MainActivity.java)
   at android.app.Activity.performCreate(Activity.java)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
   at android.app.ActivityThread.-wrap11(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
   at android.os.Handler.dispatchMessage(Handler.java)
   at android.os.Looper.loop(Looper.java)
   at android.app.ActivityThread.main(ActivityThread.java)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)

FileUtils.java

package nezibo.com.dreamception.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;

import nezibo.com.dreamception.Dreamception;
import nezibo.com.dreamception.activity.FilterActivity;

import static     android.support.v4.app.ActivityCompat.startActivityForResult;
public class FileUtils {

public static void removeTMPFiles(Context context){
    ArrayList<String> paths = new ArrayList<>();
    File dir = new File(Dreamception.GALLERY_LOCATION);
    for(File file : dir.listFiles()){
        if(file.getName().contains(FilterActivity.TEMP_FILE_MARKER)){
            paths.add(file.getAbsolutePath());
            file.delete();
        }
    }

    String[] mStringArray = new String[paths.size()];
    mStringArray = paths.toArray(mStringArray);
    MediaScannerConnection.scanFile(context, mStringArray, null, new MediaScannerConnection.OnScanCompletedListener() {
        /*
         */
        public void onScanCompleted(String path, Uri uri) {
            Log.i("ExternalStorage", "Scanned " + path + ":");
            Log.i("ExternalStorage", "-> uri=" + uri);
        }
    });
}
}

LINE 31 from sniplet above

for(File file : dir.listFiles()){

Edit, So now This is what happens now I fixed the crash at launch, its becoming clear I need to change how permissions are being assigned for the new android.

11-03 04:43:43.541 9552-9552/nezibo.com.dreamception E/libcrashlytics: Failed to find any usable unwinding libraries on the device, falling back to simple unwinding
11-03 04:43:43.676 9552-9552/nezibo.com.dreamception E/ACRA: ACRA#init called but no ReportsCrashes annotation on Application nezibo.com.dreamception
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime: FATAL EXCEPTION: ModernAsyncTask #1
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime: Process: nezibo.com.dreamception, PID: 9552
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime: java.lang.RuntimeException: An error occurred while executing doInBackground()
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:142)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:  Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=9552, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1599)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at android.content.ContentResolver.query(ContentResolver.java:491)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at android.support.v4.content.ContentResolverCompatJellybean.query(ContentResolverCompatJellybean.java:29)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at android.support.v4.content.ContentResolverCompat$ContentResolverCompatImplJB.query(ContentResolverCompat.java:57)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at android.support.v4.content.ContentResolverCompat.query(ContentResolverCompat.java:125)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:59)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:37)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:296)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:54)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:42)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:128)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
11-03 04:45:07.230 9552-10585/nezibo.com.dreamception E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818) 
11-03 04:45:07.400 9552-9693/nezibo.com.dreamception E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa1376b20
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread: Activity nezibo.com.dreamception.activity.MainActivity has leaked ServiceConnection nezibo.com.dreamception.activity.MainActivity$6@b5b5ae2 that was originally bound here
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread: android.app.ServiceConnectionLeaked: Activity nezibo.com.dreamception.activity.MainActivity has leaked ServiceConnection nezibo.com.dreamception.activity.MainActivity$6@b5b5ae2 that was originally bound here
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1092)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:986)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1303)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at android.app.ContextImpl.bindService(ContextImpl.java:1286)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at android.content.ContextWrapper.bindService(ContextWrapper.java:604)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at nezibo.com.dreamception.activity.MainActivity.initBilling(MainActivity.java:274)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at nezibo.com.dreamception.activity.MainActivity.onCreate(MainActivity.java:62)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at android.app.Activity.performCreate(Activity.java:6237)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at android.os.Looper.loop(Looper.java:148)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at java.lang.reflect.Method.invoke(Native Method)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-03 04:45:07.463 9552-9552/nezibo.com.dreamception E/ActivityThread:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-03 04:45:08.554 9552-9693/nezibo.com.dreamception E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa1377ae0

Manifest permission request.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<!-- Allows access to the flashlight -->
<permission
    android:name="android.permission.FLASHLIGHT"
    android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
    android:protectionLevel="normal" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

回答1:

You have a NullPointerException in your code. Post your code for FileUtils.removeTMPFiles() method here so we can suggest anything.

Caused by java.lang.NullPointerException: Attempt to get length of null array
  at nezibo.com.dreamception.utils.FileUtils.removeTMPFiles(FileUtils.java)

Changes suggested:

    ArrayList<String> paths = new ArrayList<>();
    File dir = new File(Dreamception.GALLERY_LOCATIONc);
    for(File file : dir.listFiles()){

to

    ArrayList<String> paths = new ArrayList<>();
    File dir = new File(Dreamception.GALLERY_LOCATION);
    if(!dir.exists())return;
    for(File file : dir.listFiles()){

+++New code for change/request in permission

if(!Settings.System.canWrite(this)) { 
    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 2909); 
} else { 
    // continue with your code 
}


回答2:

You just need to read the stacktrace:

Caused by java.lang.NullPointerException: Attempt to get length of null array
    at nezibo.com.dreamception.utils.FileUtils.removeTMPFiles(FileUtils.java:31)

So check why your array might be null in FileUtils in line 31.