0

我的错误信息是:

>02-12 10:15:34.625: E/AndroidRuntime(1018): FATAL EXCEPTION: AsyncTask
> 1 02-12 10:15:34.625: E/AndroidRuntime(1018): java.lang.RuntimeException: An error occured while executing
> doInBackground() 02-12 10:15:34.625: E/AndroidRuntime(1018):  at
> android.os.AsyncTask$3.done(AsyncTask.java:200) 02-12 10:15:34.625:
> E/AndroidRuntime(1018):   at
> java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
> 02-12 10:15:34.625: E/AndroidRuntime(1018):   at
> java.util.concurrent.FutureTask.setException(FutureTask.java:124)
> 02-12 10:15:34.625: E/AndroidRuntime(1018):   at
> java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
> 02-12 10:15:34.625: E/AndroidRuntime(1018):   at
> java.util.concurrent.FutureTask.run(FutureTask.java:137) 02-12
> 10:15:34.625: E/AndroidRuntime(1018):     at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
> 02-12 10:15:34.625: E/AndroidRuntime(1018):   at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
> 02-12 10:15:34.625: E/AndroidRuntime(1018):   at
> java.lang.Thread.run(Thread.java:1096) 02-12 10:15:34.625:
> E/AndroidRuntime(1018): Caused by: java.lang.NullPointerException
> 02-12 10:15:34.625: E/AndroidRuntime(1018):   at
> se.gibk.gibk.MainActivity$PostTask.GetList(MainActivity.java:88) 02-12
> 10:15:34.625: E/AndroidRuntime(1018):     at
> se.gibk.gibk.MainActivity$PostTask.doInBackground(MainActivity.java:143)
> 02-12 10:15:34.625: E/AndroidRuntime(1018):   at
> se.gibk.gibk.MainActivity$PostTask.doInBackground(MainActivity.java:1)
> 02-12 10:15:34.625: E/AndroidRuntime(1018):   at
> android.os.AsyncTask$2.call(AsyncTask.java:185) 02-12 10:15:34.625:
> E/AndroidRuntime(1018):   at
> java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

我的代码是:

 public class MainActivity extends Activity {

private static final String TAG = "Debugging";
private ListView list;
ArrayList<String> links;
Sermons sermons;


protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "MainActivity - OnCreate()");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new PostTask(this).execute("http://gibk.se/sample-page/predikningar/?podcast");
    Log.d(TAG, "execute");      

}

公共类 PostTask 扩展 AsyncTask> {

  private Context context;

  public PostTask(Context context) {
        this.context = context;

   }

 private ArrayList<Sermons> GetList(String url) {
        Log.d(TAG, "GetList");
        ArrayList<Sermons> results = new ArrayList<Sermons>();

           try {

                URL urls = new URL(url);


                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(false);
                XmlPullParser xmlParser = factory.newPullParser();
                xmlParser.setInput(this.getInputStream(urls), "UTF_8");

                boolean insideItem = false;

                int eventType = xmlParser.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) {

                    if (eventType == XmlPullParser.START_TAG) {

                        if (xmlParser.getName().equalsIgnoreCase("item")) {
                            sermons = new Sermons();
                            insideItem = true;

                         } else if (xmlParser.getName().equalsIgnoreCase("title")) {

                             if (insideItem)
                                 sermons.setSermon(xmlParser.nextText()); 

                         } else if (xmlParser.getName().equalsIgnoreCase("itunes:author")) {

                             if (insideItem)
                                 sermons.setPreacher(xmlParser.nextText()); 

                         } else if (xmlParser.getName().equalsIgnoreCase("guid")) {
                             if (insideItem)
                               links.add(xmlParser.nextText());   

                         }

                        results.add(sermons);

                    }

                    else if(eventType==XmlPullParser.END_TAG && xmlParser.getName().equalsIgnoreCase("item")){

                        insideItem=false;
                    }

                     eventType = xmlParser.next();
                }

            }

             catch (MalformedURLException e) {
                 e.printStackTrace();

             }

            catch (XmlPullParserException e) {
                e.printStackTrace();

            }

            catch (IOException e) {
                e.printStackTrace();

            }

            return results;

    }

   private InputStream getInputStream(URL url) {

        try {

            return url.openConnection().getInputStream();

        } catch (IOException e) {

            return null;

        }
   }


@Override
protected ArrayList<Sermons> doInBackground(String... params) {
    Log.d(TAG, "doInBackGround");
    String url = params[0];
    ArrayList<Sermons> sermon = this.GetList(url);  
    return sermon;      

}

 @Override
 protected void onPostExecute(ArrayList<Sermons> result) {
         super.onPostExecute(result);
         Log.d(TAG, "onPostExecute");
        links = new ArrayList<String>();    
        list = (ListView) findViewById(R.id.list);
        list.setAdapter(new Adapter(MainActivity.this, (ArrayList<Sermons>) result));

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position,
                    long arg3) {

                Uri uri = Uri.parse(links.get(position));
                String url = uri.toString();

                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);

            }

        });
 }

} }

4

1 回答 1

0

不确定它是否相关,但:

"title".equalsIgnoreCase(xmlParser.getName())

优于:

xmlParser.getName().equalsIgnoreCase("title")

减少 NPE 的机会。

另一个问题:

  private InputStream getInputStream(URL url) {

        try {

            return url.openConnection().getInputStream();

        } catch (IOException e) {

            return null;

        }
   }

不应该在异常时返回 null,而是抛出异常。

于 2013-02-12T10:38:28.610 回答