Grux
Grux

Reputation: 584

OnPostExecute not getting string

The contents of the string descript is showing up in logcat with no problems but won't display on the textview in the dialog located in OnPostExecute. It's a nullpointer exception. I'm not sure why since the episode string works just fine. Can anyone tell me why?

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();
        }
    }

Upvotes: 0

Views: 42

Answers (1)

peshkira
peshkira

Reputation: 6229

You should use

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();

Since the textview is not in the layout of the activity but in the layout of the dialog. In your code, descrip is null, because it cannot be found.

Upvotes: 1

Related Questions