开发者

how to solve imageadpter error?

开发者 https://www.devze.com 2023-03-03 06:01 出处:网络
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....

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消