qianzhi008 2011-05-10 11:28
浏览 459
已采纳

Unable to start activity componentInfo

各位帮忙看一下,新手,急。。。。
启动程序时抛出这样的异常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]

  • 写回答

2条回答 默认 最新

  • iteye_8576 2011-05-10 14:50
    关注

    是不是 路径"/sdcrad/photo/" 没有文件?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog