gpt4 book ai didi

java - 为什么我会收到 NullPointerException?

转载 作者:行者123 更新时间:2023-12-01 12:00:26 25 4
gpt4 key购买 nike

这是我的面部检测 Android 应用程序的代码。我收到错误,但不知道为什么

private int count;
private Bitmap[] thumbnails;
private Bitmap[] arcok;
private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter;
private int faceCount;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID};
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);

int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
this.arrPath = new String[this.count];
this.thumbnailsselection = new boolean[this.count];



for (int i = 0; i < thumbnails.length - 1; ++i) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);

thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(),
id,
MediaStore.Images.Thumbnails.MICRO_KIND,
null);

if (hasFace(thumbnails[i]) == true) {

for (int j = 0; j < this.count; j++) {
imagecursor.moveToPosition(i);

int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(),
id,
MediaStore.Images.Thumbnails.MICRO_KIND,
null);
arrPath[i] = imagecursor.getString(dataColumnIndex);
faceCount++;
}
} else
thumbnails[i] = null;


}
this.arcok = new Bitmap[faceCount];
for (int i = 0, j = 0; i < thumbnails.length; ++i) {
if (thumbnails[i] != null) {
arcok[j] = thumbnails[i];
j++;
}
}


GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
imagecursor.close();

final Button selectBtn = (Button) findViewById(R.id.selectBtn);


}

private boolean hasFace(Bitmap inBmp) {
Bitmap bmp = inBmp.copy(Bitmap.Config.RGB_565, true);
FaceDetector fd = new FaceDetector(bmp.getWidth(), bmp.getHeight(), 1);
FaceDetector.Face faces[] = new FaceDetector.Face[1];
int numFaces = fd.findFaces(bmp, faces);
bmp.recycle();
return numFaces > 0;
}

这是日志猫:

01-18 23:04:59.719  28915-28915/com.img.user.javitott E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.img.user.javitott/com.img.user.javitott.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3768)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.img.user.javitott.MainActivity.hasFace(MainActivity.java:124)
at com.img.user.javitott.MainActivity.onCreate(MainActivity.java:59)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
            at android.app.ActivityThread.access$1500(ActivityThread.java:121)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3768)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
            at dalvik.system.NativeStart.main(Native Method)

我在 hasFace 方法的第一行和第一个 for 循环的 if 语句中收到错误

最佳答案

在您的方法 hasFace 中,您从不检查以确保 inBmp 不为 NULL,因此当您尝试访问 Config 属性,将会失败。

关于java - 为什么我会收到 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28015354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com