1

このクラスで非同期タスクを実装しようとしてgetInputStreamいますが、プログラムで関数を呼び出して値を返しているので、どこに配置すればよいかわかりません。getInputStream非同期タスクのどこで定義する必要がありますか?以下の例外が発生します

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity      com.sparkplug.xxxx}: java.lang.NullPointerException

以下は私のメインクラスです:

public class abcreaderextends ListActivity {

    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

    parsing p=new parsing();

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_awsreader);
        ((PullToRefreshListView) getListView())
                .setOnRefreshListener(new OnRefreshListener() {
                    public void onRefresh() {
                        // Do work to refresh the list here.
                        new GetDataTask().execute();
                    }
                });
        InputStreamOperation in= new InputStreamOperation();

        in.execute();

        //p.parse();

        for (int i = 0; i < p.headlines.size(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            // adding each child node to HashMap key => value
            map.put("title", p.headlines.get(i));
            map.put("dcdate", p.lstDate.get(i));
            map.put("description", p.description.get(i));
            // adding HashList to ArrayList
            menuItems.add(map);
        }

                ListAdapter rssFeedSection = new SimpleAdapter(this, menuItems,
                R.layout.list_item, new String[] { "title", "dcdate",
                        "description" }, new int[] { R.id.name, R.id.date1,
                        R.id.desc });
        setListAdapter(rssFeedSection);
    }

    class GetDataTask extends AsyncTask<Void, Void, String[]> {
        @Override
        protected void onPostExecute(String[] result) {
            // **menuItems.addFirst("Added after refresh...");
            // Call onRefreshComplete when the list has been refreshed.
            ((PullToRefreshListView) getListView()).onRefreshComplete();
            super.onPostExecute(result);
        }

        @Override
        protected String[] doInBackground(Void... params) {
            // TODO Auto-generated method stub
            return null;
        }
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Uri uri = Uri.parse((String) p.links.get(position));
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }
}

これは私の構文解析クラスです。publicclassparsing{リストの見出し; リンクを一覧表示します。リストの説明; リストlstDate; newDateを一覧表示します。//文字列a、b、c、d; public InputStream getInputStream(URL url){try {return url.openConnection()。getInputStream(); } catch(IOException e){return null; }}

    public HashMap<String, ArrayList<String>> parse() {

        // Initializing instance variables
        headlines = new ArrayList<String>();
        links = new ArrayList<String>();
        description = new ArrayList<String>();
        lstDate = new ArrayList<String>();
        try {
            URL url = new URL(
                    "http://feeds.feedburner.com/xxxx");
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(false);
            XmlPullParser xpp = factory.newPullParser();
            // We will get the XML from an input stream
            xpp.setInput(getInputStream(url), "UTF_8");
            int i = 0;
            boolean insideItem = false;
            // Returns the type of current event: START_TAG, END_TAG, etc..
            int eventType = xpp.getEventType();
            int k = 0;
            while (eventType != XmlPullParser.END_DOCUMENT) {
                i++;
                // Log.i("Tag : ",xpp.getName().toString());
                // Log.i("Text : ",xpp.nextText().toString());
                if (eventType == XmlPullParser.START_TAG) {
                    Log.i("Tag : ", xpp.getName().toString());
                    // Log.i("Text : ",xpp.nextText().toString());
                    if (xpp.getName().equalsIgnoreCase("item")) {
                        insideItem = true;
                    } else if (xpp.getName().equalsIgnoreCase("title")) {
                        if (insideItem) {
                            String var = xpp.nextText().toString();
                            headlines.add(var); // extract the description of
                                                // article
                            Log.i("Title : ", var);
                            // Log.i("Count : ",i+"");
                        }
                    } else if (xpp.getName().equalsIgnoreCase("description")) {
                        if (insideItem) {
                            String desc = xpp.nextText().toString();
                            description.add(desc); // extract the description of
                                                    // article
                            Log.i("Desc : ", desc);
                        }
                    } else if (xpp.getName().equalsIgnoreCase("dc:date")) {
                        if (insideItem) {
                            String strDate = xpp.nextText().toString();

                            System.out.println("rahul"+strDate.substring(0,10));
                            //lstDate = Arrays.asList(arr[k].substring(0,10));
                            lstDate.add(strDate.substring(0,10));
                            System.out.println("lstDate"+lstDate);
                            k = k+1;
                            Log.i("Date : ", strDate);
                        }
                    } else if (xpp.getName().equalsIgnoreCase("link")) {
                        if (insideItem)
                            links.add(xpp.nextText()); // extract the link of
                                                        // article
                    }
                } else if (eventType == XmlPullParser.END_TAG
                        && xpp.getName().equalsIgnoreCase("item")) {
                    insideItem = false;
                }
                eventType = xpp.next(); // move to next element
            }// While end
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        HashMap<String, ArrayList<String>> alllists = 
                                        new HashMap<String, ArrayList<String>>();
        alllists.put("headlines",(ArrayList<String>) headlines);
        alllists.put("links",(ArrayList<String>) links);
        alllists.put("description",(ArrayList<String>) description);
        alllists.put("lstDate",(ArrayList<String>) lstDate);

        return alllists;
        }
}

これは私のInputStreamOperationクラスです:public class InputStreamOperation extends AsyncTask >> {

@Override
protected void onPreExecute() {
// show progress bar here(have not used any progress bar)
}

@Override
protected HashMap<String, ArrayList<String>> 
                           doInBackground(Void... params) {

//call parse() method here
parsing parsingobj=new parsing();
HashMap<String, ArrayList<String>> alllists=parsingobj.parse();

return alllists;  //<<< retun final result from here

}      

@Override
protected void onPostExecute(HashMap<String, ArrayList<String>> result) {  
// update UI here            
}

}
4

3 に答える 3

1

このようにしてみてください。

class Search AsyncTask<String, Void, ArrayList<Movie>>() {

                @Override
                protected void onPreExecute() {
                    progressDialog= ProgressDialog.show(context, "Please Wait","Searching movies", true);
                }

                @Override
                protected ArrayList<Movie> doInBackground(String... params) {
                    String moviesJson = retrieveStream[params[0]];
                    JSONObject moviesJson = new JSONObject(moviesJson);
                    ArrayList<Movie> movies = new ArrayList<Movie>();
                    /*
                     * Do your code to process the JSON and create an ArrayList of films.
                     * It's just a suggestion how to store the data.
                     */
                    return movies;
                }

                protected void onPostExecute(ArrayList<Movie> result) {
                    progressDialog.dismiss();
                    //create a method to set an ArrayList in your adapter and set it here.
                    sampleActivity.mListAdapter.setMovies(result);
                    sampleActivity.mListAdapter.notifyDataSetChanged();
                }
            }

詳細については..

AsyncTaskとInputStreamに関するAndroidの問題

于 2013-01-22T10:29:55.763 に答える
0

AsyncTaskのgetInputStreaminsideメソッドを次のように呼び出す必要があります。doInBackground

まず、解析メソッドの戻りタイプを次のように変更HashMap<String, String>します。

public HashMap<String, ArrayList<String>> parse() {

 ///your code here...

HashMap<String, ArrayList<String>> alllists = 
                                new HashMap<String, ArrayList<String>>();
alllists.put("headlines",headlines);
alllists.put("links",links);
alllists.put("description",description);
alllists.put("lstDate",lstDate);

return alllists;
}

AsyncTaskクラスを次のように作成します。

private class InputStreamOperation extends 
                    AsyncTask<String, Void, HashMap<String, ArrayList<String>>> {

      @Override
      protected void onPreExecute() {
        // show progress bar here
      }

      @Override
      protected HashMap<String, ArrayList<String>> 
                                               doInBackground(String... params) {

           //call parse() method here
          parsing parsingobj=new parsing();
          HashMap<String, ArrayList<String>> alllists=parsingobj.parse();

          return alllists;  //<<< retun final result from here

      }      

      @Override
      protected void onPostExecute(HashMap<String, ArrayList<String>> result) {  
              // update UI here            
      }


}
于 2013-01-22T10:31:49.570 に答える
0

doInBackground()メソッドで記述する必要があります。

フォーマットの問題により、正確なコードをアップロードできません

1asyncTaskを拡張するクラスを作成します。2doInBackgroundにgetInputStreamを書き込みます。3asynctaskを呼び出してInputStreamを取得します。

于 2013-01-22T10:29:59.403 に答える