爲什麼我在fn = list.getSelectedItem().toString();
處遇到NullPointerException。保存所選項目的ListView時的NPE
public class FileList extends Activity{
ListView list;
String fn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filelist);
list = (ListView) findViewById(R.id.lv1);
String pth = Environment.getExternalStorageDirectory().getPath();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getListOfFiles(pth));
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0,
View arg1, int position, long arg3)
{
System.out.println(list.getSelectedItem().toString());
fn = list.getSelectedItem().toString(); //NPE Here
}
});
}
theres在getSelectedItem()的第5行上輸入了一個錯字。 – r2DoesInc 2014-08-27 13:32:04
@ r2DoesInc是什麼?它看起來像我變得瞎了:D你能爲我找到它:P – 2014-08-27 13:33:25
Fn宣佈在哪裏?請注意,您不能修改在OnItemClickListener範圍之外聲明的變量的值,並且只有當它們聲明爲「最終」時纔可以讀取它們的值。 – mittelmania 2014-08-27 13:34:19