所以我对Java的经验很少,但我可以快速学习。在Marshmallow 6.0中启动应用程序时。它突然崩溃了。我可以从Android Studio中的logcat获取此内容
跳过被纠正的文字
E / AndroidRuntime:致命异常:主要
E / AndroidRuntime:进程:nezibo.com.dreamception,PID:1863
E / AndroidRuntime:java.lang.RuntimeException:无法启动活动ComponentInfo {nezibo.com.dreamception / nezibo.com.dreamception.activity.MainActivity}:java.lang.NullPointerException:尝试获取null数组的长度
E / AndroidRuntime:在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
E / AndroidRuntime:在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
E / AndroidRuntime:在android.app.ActivityThread.-wrap11(ActivityThread.java)
E / AndroidRuntime:在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)
E / AndroidRuntime:在android.os.Handler.dispatchMessage(Handler.java:102)
E / AndroidRuntime:在android.os.Looper.loop(Looper.java:148)
E / AndroidRuntime:在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:引起:java.lang.NullPointerException:尝试获取null数组的长度
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:在android.app.Activity.performCreate(Activity.java:6237)
E / AndroidRuntime:在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
E / AndroidRuntime:在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
E / AndroidRuntime:在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
E / AndroidRuntime:在android.app.ActivityThread.-wrap11(ActivityThread.java)
E / AndroidRuntime:在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)
E / AndroidRuntime:在android.os.Handler.dispatchMessage(Handler.java:102)
E / AndroidRuntime:在android.os.Looper.loop(Looper.java:148)
E / AndroidRuntime:在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:在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
CRASHLYTICS显示
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);
}
});
}
}
上面的小册子中的第31行
for(File file : dir.listFiles()){
编辑,所以现在这就是现在发生的事情我在发布时修复了崩溃,它变得清晰了我需要改变为新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
明确权限请求。
<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" />
答案 0 :(得分:2)
你只需要阅读stacktrace:
null
因此,请检查第31行的FileUtils中数组可能为''
的原因。
答案 1 :(得分:0)
您的代码中有NullPointerException
。在此处发布FileUtils.removeTMPFiles()
方法的代码,以便我们提出建议。
Caused by java.lang.NullPointerException: Attempt to get length of null array
at nezibo.com.dreamception.utils.FileUtils.removeTMPFiles(FileUtils.java)
建议更改:
ArrayList<String> paths = new ArrayList<>();
File dir = new File(Dreamception.GALLERY_LOCATIONc);
for(File file : dir.listFiles()){
到
ArrayList<String> paths = new ArrayList<>();
File dir = new File(Dreamception.GALLERY_LOCATION);
if(!dir.exists())return;
for(File file : dir.listFiles()){
+++许可中的变更/请求的新代码
if(!Settings.System.canWrite(this)) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 2909);
} else {
// continue with your code
}