Saturday, 28 September 2013

handler throws NullPointerException after initialtion

handler throws NullPointerException after initialtion

Here is the key code:
public class ChooseFileDialog extends DMBaseConfirmDialog {
public static final int ALL_FILES_READY = 1;
public static final int LIST_FILES = ALL_FILES_READY + 1;
private FileListView listView = null;
private EditText searchContact = null;
private List<FileDescripter> fileDescripters = null;
private DMHandler handler = null;
public ChooseFileDialog(Context context, int theme) {
super(context, theme);
}
private void init(Context context) {
handler = new DMHandler(context) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case ALL_FILES_READY:
System.out.println("listView ready?" + listView);
// listView.getAdapter().addItems(fileDescripters);
break;
case LIST_FILES:
// String path = (String) msg.obj;
// listView.getAdapter().clearItems();
// listFiles(new File(path));
break;
default:
break;
}
super.handleMessage(msg);
}
};
System.out.println("list files");
listFiles(new File("/"));
}
private void listFiles(final File path) {
System.out.println("handler1:" + handler);
new Thread() {
@Override
public void run() {
fileDescripters = new ArrayList<FileDescripter>();
File[] files = path.listFiles();
File parent;
if ((parent = path.getParentFile()) != null) {
fileDescripters.add(new FileDescripter(parent));
}
if (files != null)
for (File file : files) {
fileDescripters.add(new FileDescripter(file));
}
System.out.println("handler run:" + handler);
handler.sendEmptyMessage(ALL_FILES_READY);
}
}.start();
}
@Override
protected void updateView(View root, Context context) {
listView = (FileListView) root.findViewById(R.id.file_list_view);
System.out.println("listview 1:" + listView);
searchContact = (EditText) root.findViewById(R.id.search_file);
searchContact.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
List<FileDescripter> searchsContacts =
searchContacts(searchContact
.getText().toString().trim());
listView.getAdapter().clearItems();
listView.getAdapter().addItems(searchsContacts);
}
});
init(context);
}
super(context, theme); will call updateView(View root, Context context)

after initiating the views, init(context) will be called to initiate handler

then listFiles will be called

strangely, from the log :

09-29 10:00:27.900: I/System.out(4554): handler1:Handler
09-29 10:00:27.965: I/System.out(4554): handler run:null

how the handler could be null after initiation.

No comments:

Post a Comment