OnPostExecute没有获取字符串

时间:2014-04-11 20:36:39

标签: java android android-asynctask

字符串descript的内容在logcat中显示没有问题,但不会显示在OnPostExecute对话框中的textview上。它是一个nullpointer异常。我不确定为什么因为episode字符串工作得很好。谁能告诉我为什么?

class getDescContent extends AsyncTask<Void, Void, Void> {
        FileInputStream input = null;
        String episode = null;
        String descript = null;

        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                input = new FileInputStream(descFile);
                BufferedReader br = new BufferedReader(new InputStreamReader(input));
                String line;
                while ((line = br.readLine()) != null) {
                    if (line.length() < 50) {
                        episode = line;
                        continue;
                    } 
                    descript = line;
                    Log.i("", descript);
                }
                input.close();
                br.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void v) {
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.info_dialog);
            dialog.setTitle(episode);
            TextView descrip = (TextView) findViewById(R.id.dialog_text);
            descrip.setText(descript);
            dialog.show();
        }
    }

1 个答案:

答案 0 :(得分:1)

你应该使用

final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.info_dialog);
dialog.setTitle(episode);
TextView descrip = (TextView) dialog.findViewById(R.id.dialog_text); //change in this line
descrip.setText(descript);
dialog.show();

由于textview不在活动的布局中,而是在对话框的布局中。 在您的代码中,descriptp为null,因为无法找到它。