如何解决imageadpter错误?

发布于 2024-11-04 15:30:22 字数 4467 浏览 4 评论 0原文

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

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

hi now i am trying to create folder in my gallery but my application crash in this line...how to solve these error please help me....

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

code:

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 error:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里人 2024-11-11 15:30:23

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

这对我来说看起来是错误的:

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

但是,它与 这个示例 所以我确定没问题。

如果您的应用程序在这一行崩溃(您已经说过):

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

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

那么,明显的可能性是:

  1. 此时 imagegrid 是否为 null?如果是这样,调用它的方法将导致空异常。您可能已在某处创建并初始化了它,但您没有包含该代码。
  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 设置适配器而被调用。

I don't think I really understand the question... However, you're getting a null pointer exception.

This looks wrong to me:

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

However, it's the same as this example so I'm sure it is fine.

If you application is crashing at this line (which you've said):

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

Then is seems like, either there must be a null on that line, or a side effect of one of the calls causes the exception.

So, obvious possibilities:

  1. Is imagegrid null at this point? If so, invoking a method on it will cause a null exception. You've presumably created and initialized it somewhere, but you don't include that code.
  2. Does getApplicationContext return non-null? It's assigned within the constructor for ImAdapterh, but not used by the constructor, but it's worth checking.

Your ImAdapterh contains the following lines:

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

This seems error prone to me, although it may be stylistic... I'd try changing it to:

File dir;
int count;
String[] fileNames;

Then construct the objects in your constructor for the class. This may also give you a better stack trace if one of these lines is causing the problem (and allows you to set a breakpoint on the constructor code).

I'd also set a breakpoint at the start of your getView method, to see if it is being called as a result of your setting the adapter for the imagegrid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文