在我的应用程序中,我正在从 sd 卡中检索文件。并将其列在列表视图中。同时列出 m 得到一个异常致命的执行。我该怎么办。请帮帮我。我正在使用 android2.2。相同的代码在 2.3 上运行
下面是代码-
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//i=getIntent();
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator
+ "gallery" //folder name
);
if (!file.exists()) {
file.mkdirs();
}
ListView lv = (ListView) findViewById(R.id.listview);
list = getSD();
lv.setAdapter((ListAdapter) new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));//here i'm getting exeception
Log.d("LOG", "FIRE !!!! " );
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
i=new Intent();
i.putExtra("file","string");
setResult(RESULT_OK, i);
finish();
}
});
}
例外-
02-14 14:27:38.377: E/AndroidRuntime(395): FATAL EXCEPTION: main
02-14 14:27:38.377: E/AndroidRuntime(395): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.zproject/com.my.zproject.ImportFile}: java.lang.NullPointerException
02-14 14:27:38.377: E/AndroidRuntime(395): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-14 14:27:38.377: E/AndroidRuntime(395): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-14 14:27:38.377: E/AndroidRuntime(395): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-14 14:27:38.377: E/AndroidRuntime(395): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-14 14:27:38.377: E/AndroidRuntime(395): at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 14:27:38.377: E/AndroidRuntime(395): at android.os.Looper.loop(Looper.java:123)
02-14 14:27:38.377: E/AndroidRuntime(395): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-14 14:27:38.377: E/AndroidRuntime(395): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 14:27:38.377: E/AndroidRuntime(395): at java.lang.reflect.Method.invoke(Method.java:521)
02-14 14:27:38.377: E/AndroidRuntime(395): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-14 14:27:38.377: E/AndroidRuntime(395): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-14 14:27:38.377: E/AndroidRuntime(395): at dalvik.system.NativeStart.main(Native Method)
02-14 14:27:38.377: E/AndroidRuntime(395): Caused by: java.lang.NullPointerException
02-14 14:27:38.377: E/AndroidRuntime(395): at com.my.zproject.ImportFile.onCreate(ImportFile.java:38)
02-14 14:27:38.377: E/AndroidRuntime(395): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)