在我的应用程序中,我正在从sd card重新获取文件,并将其列在listview中。 列出m得到异常致命的execption。我应该怎么做。请帮助我。我使用android2.2.same代码运行在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)
答案 0 :(得分:0)
Caused by: java.lang.NullPointerException
02-14 14:27:38.377: E/AndroidRuntime(395): at com.my.zproject.ImportFile.onCreate(ImportFile.java:38)
您的方法NullPointerException
中的ImportFile
课程中有一个onCreate(...)
38 。
答案 1 :(得分:0)
问题是因为activity_main.xml。请检查文件名/变量名是否正确。