gpt4 book ai didi

java - 如何解决imageadpter错误?

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

嗨,现在我正在尝试在我的画廊中创建文件夹,但我的应用程序在这一行崩溃...如何解决这些错误,请帮助我...

imagegrid.setAdapter(new ImAdapterh(getApplicationContext()));
System.out.println("index"+getApplicationContext());

代码:

public class ImAdapterh extends BaseAdapter{

File dir=new File(Environment.getExternalStorageDirectory(),"/Pictures/");

int count=dir.list().length;

String[] fileNames = dir.list();

private Context mContext;

public ImAdapterh(Context c) {
mContext = c;
}

public int getCount() {
return count;
}

public Object getItem(int position) {
return null;
}

public long getItemId(int position) {
return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;


for(String bitmapFileName : fileNames)
{
if (convertView == null)
{ // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new Gallery.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);

Bitmap bmp = BitmapFactory.decodeFile(dir.getPath() + "/" +
bitmapFileName);
System.out.println(dir);
imageView.setImageBitmap(bmp);
}else
{
imageView = (ImageView) convertView;

}
}
return imageView;
}
}}

logcat错误:

05-03 22:37:50.134: DEBUG/AndroidRuntime(1328): Shutting down VM
05-03 22:37:50.134: WARN/dalvikvm(1328): threadid=1: thread exiting with uncaught
exception (group=0x4001d800)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): FATAL EXCEPTION: main
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): java.lang.RuntimeException: Unable to
start activity
ComponentInfo{ImageViewExample.ImageViewExample/ImageViewExample
.ImageViewExample.ImageViewExample}: java.lang.NullPointerException
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
android.os.Handler.dispatchMessage(Handler.java:99)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
android.os.Looper.loop(Looper.java:123)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
android.app.ActivityThread.main(ActivityThread.java:4627)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
java.lang.reflect.Method.invokeNative(Native Method)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
java.lang.reflect.Method.invoke(Method.java:521)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
dalvik.system.NativeStart.main(Native Method)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): Caused by:
java.lang.NullPointerException
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
ImageViewExample.ImageViewExample.ImageViewExample$ImAdapterh.<init>
(ImageViewExample.java:72)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
ImageViewExample.ImageViewExample.ImageViewExample
.init_phone_image_grid(ImageViewExample.java:49)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
ImageViewExample.ImageViewExample.ImageViewExample
.onCreate(ImageViewExample.java:36)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-03 22:37:50.184: ERROR/AndroidRuntime(1328): ... 11 more

最佳答案

我认为我并没有真正理解这个问题...但是,您遇到了空指针异常。

这对我来说看起来不对:

public Object getItem(int position) 
{
return null;
}

但是,它与 this example 相同所以我确信这没问题。

如果您的应用程序在这一行崩溃(正如您所说):

imagegrid.setAdapter(new ImAdapterh(getApplicationContext()));

那么看起来,要么该行必须有空值,要么其中一个调用的副作用导致异常。

所以,明显的可能性:

  1. 此时 imagegrid 是否为空?如果是这样,调用它的方法将导致空异常。您可能已在某处创建并初始化了它,但您没有包含该代码。
  2. getApplicationContext 返回非空值吗?它是在 ImAdapterh 的构造函数中分配的,但构造函数不使用它,但值得检查。

您的 ImAdapterh 包含以下行:

File dir=new File(Environment.getExternalStorageDirectory(),"/Pictures/");
int count=dir.list().length;
String[] fileNames = dir.list();

这对我来说似乎很容易出错,尽管它可能是风格上的......我会尝试将其更改为:

File dir;
int count;
String[] fileNames;

然后在类的构造函数中构造对象。如果这些行之一导致问题,这也可能为您提供更好的堆栈跟踪(并允许您在构造函数代码上设置断点)。

我还在 getView 方法的开头设置了一个断点,以查看它是否由于您为 imagegrid 设置适配器而被调用.

关于java - 如何解决imageadpter错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5880203/

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