各位帮忙看一下,新手,急。。。。
启动程序时抛出这样的异常Unable to start activity ComponentInfo{com.kevin.test/com.kevin.test.Ex_07}:java.lang.NullPointerException
代码如下:
[code="java"]
public class Ex_07 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
Gallery gallery = (Gallery)findViewById(R.id.mygallery);
gallery.setAdapter(new ImageAdapter(this,getSD()));
}
private List getSD(){
List it = new ArrayList();
File file = new File("/sdcrad/photo/");
File[] files = file.listFiles();
for(int i = 0 ; i < files.length; i++){
File f = files[i];
if(getImageFile(f.getPath()))
it.add(f.getPath());
}
return it;
}
private boolean getImageFile(String fname){
boolean re;
String end = fname.substring(fname.lastIndexOf(".")+1,fname.length()).toLowerCase();
if(end.equals("jpg")||end.equals("gif")||end.equals("png")||end.equals("jpeg")||end.equals("bmp")){
re = true;
}else{
re = false;
}
return re;
}
}
[/code]
[code="java"]
package com.kevin.test.adapter;
import java.util.List;
import com.kevin.test.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private int mGalleryItemBackground;
private Context mContext;
private List list;
public ImageAdapter(Context c,List<String> list){
mContext = c;
this.list = list;
// get the properties defined in attrs.xml
TypedArray typedArray = mContext.obtainStyledAttributes(R.styleable.Gallery);
// get Gallery's id
mGalleryItemBackground = typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
typedArray.recycle();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(mContext);
Bitmap bm = BitmapFactory.decodeFile(list.get(position).toString());
i.setImageBitmap(bm);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(136, 88));
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
[/code]
androidMainifest.xml文件
[code="java"]
<?xml version="1.0" encoding="utf-8"?>
package="com.kevin.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Ex_07"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
[/code]