onClickListener崩溃Android应用程序

时间:2014-05-10 13:12:59

标签: android listview onclicklistener

我正在写一个基本的笔记记录应用程序,我试图从ListView实现加载功能。以下代码来自load函数,它填充并侦听ListView上的点击。

for(String tmpf : fileNames) {
    ListView notesList = (ListView) findViewById(R.id.listNotes);
    arrayAdapter = new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_1, fileNames);
    notesList.setAdapter(arrayAdapter);

    Toast.makeText(getApplicationContext(), "-1", Toast.LENGTH_SHORT).show();

    notesList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id) {

            Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();

            TextView clickedView = (TextView) view;
            String loadLabel = clickedView.getText().toString();
            StringBuilder sb = new StringBuilder();

            Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();

            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(openFileInput(loadLabel)));
                String line = null;

                Toast.makeText(getApplicationContext(), "3", Toast.LENGTH_SHORT).show();

                while ((line = reader.readLine()) != null) {
                    sb.append(line).append("\n");
                }
            } catch(OutOfMemoryError om) {
                Toast.makeText(getApplicationContext(), "Error loading note - Out of Memory", Toast.LENGTH_LONG).show();
                om.printStackTrace();
            } catch(Exception e) {
                Toast.makeText(getApplicationContext(), "Error loading note - Exception", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }

            Toast.makeText(getApplicationContext(), "4", Toast.LENGTH_SHORT).show();

            String notes = sb.toString();
            EditText noteLabelField = (EditText) findViewById(R.id.editHeading);
            EditText textField = (EditText) findViewById(R.id.editNote);

            Toast.makeText(getApplicationContext(), "5", Toast.LENGTH_SHORT).show();

            noteLabelField.setText(loadLabel);
            textField.setText(notes);

            Toast.makeText(getApplicationContext(), "6", Toast.LENGTH_SHORT).show();

        }
    });

    notesList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parentAdapter, View view, int position, long id) {
            TextView clickedView = (TextView) view;
            deleteFile(clickedView.getText().toString());
            Toast.makeText(getApplicationContext(), "Note deleted", Toast.LENGTH_SHORT).show();
            return true;
        }
    });
}

问题是,只要点击列表中的任何内容,活动就会崩溃。使用Toast功能,我发出了一些警报,我只进入&#34; -1&#34;。我甚至无法在点击某个项目时立即进入下一个Toast。

更奇怪的是,上面代码末尾的长按一下监听器工作得很好。

有什么想法吗?

以下是LogCat的错误行:

05-10 13:00:58.420: D/AndroidRuntime(781): Shutting down VM
05-10 13:00:58.420: W/dalvikvm(781): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-10 13:00:58.440: E/AndroidRuntime(781): FATAL EXCEPTION: main
05-10 13:00:58.440: E/AndroidRuntime(781): java.lang.NullPointerException
05-10 13:00:58.440: E/AndroidRuntime(781):  at com.example.mobileappassessment.LoadActivity$1.onItemClick(LoadActivity.java:105)
05-10 13:00:58.440: E/AndroidRuntime(781):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-10 13:00:58.440: E/AndroidRuntime(781):  at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
05-10 13:00:58.440: E/AndroidRuntime(781):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
05-10 13:00:58.440: E/AndroidRuntime(781):  at android.widget.AbsListView$1.run(AbsListView.java:3423)
05-10 13:00:58.440: E/AndroidRuntime(781):  at android.os.Handler.handleCallback(Handler.java:725)
05-10 13:00:58.440: E/AndroidRuntime(781):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 13:00:58.440: E/AndroidRuntime(781):  at android.os.Looper.loop(Looper.java:137)
05-10 13:00:58.440: E/AndroidRuntime(781):  at android.app.ActivityThread.main(ActivityThread.java:5041)
05-10 13:00:58.440: E/AndroidRuntime(781):  at java.lang.reflect.Method.invokeNative(Native Method)
05-10 13:00:58.440: E/AndroidRuntime(781):  at java.lang.reflect.Method.invoke(Method.java:511)
05-10 13:00:58.440: E/AndroidRuntime(781):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-10 13:00:58.440: E/AndroidRuntime(781):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-10 13:00:58.440: E/AndroidRuntime(781):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

在onClickListener中调用findViewById并不好。

尝试在您的活动中创建私人字段EditText noteLabelField,填写它 EditText noteLabelField = (EditText) findViewById(R.id.editHeading);中的onCreate,并在onClickListener

中使用引用noteLabelField

答案 1 :(得分:0)

答案是盯着你。 loadlabel是造成我认为麻烦的那个,但是请发布可能指定NullPointerException行的整个日志cat,并确保loadlabelBufferedReader reader = new BufferedReader(new InputStreamReader(openFileInput(loadLabel)));输入时不为空findViewById 1}}。尝试在将loadlabel发送到BufferedReader之前打印它,并确实全局声明所有需要的变量,这样就不会在onClicks内使用{{1}}。尝试这些,他们可能会工作。快乐的编码。