异步方法。我如何让它工作

时间:2014-09-22 01:58:33

标签: java android multithreading android-asynctask rss

所以我的应用程序在尝试从在线提取Rss Feed之前不会崩溃。我需要知道如何使它工作。

这是我的活动代码:

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;    
import java.util.List;    

public class VehicleRecall extends Activity {

    // A reference to the local object
    private VehicleRecall local;
    private Thread thread;        

    //method creates application view
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        // Set view
        setContentView (R.layout.activity_vehiclerecall);

        //Set reference to this activity
        local = this;

        GetRSSDataTask task = new GetRSSDataTask ();

        //Start download RSS task
        task.execute ("http://www-odi.nhtsa.dot.gov/rss/feeds/rss_recalls_V.xml");           
    }

    private  class GetRSSDataTask extends AsyncTask <String, Void, List<RssItem> > {

        @Override
        protected List<RssItem> doInBackground(String... urls) {               

            try {
                // Create RSS reader
                RssReader rssReader = new RssReader (urls[0]);

                // Parse RSS, get items
                return rssReader.getItem ();

            } catch (Exception e) {
                Log.e ("CANVehicleRecall", e.getMessage ());
            }
            return null;
        }

        @Override
        protected void onPostExecute(List<RssItem> result) {

            //Get a ListView from main view
            ListView canItems = (ListView) findViewById (R.id.recallList);

            // Create a list adapter
            ArrayAdapter<RssItem> adapter =
            new ArrayAdapter<RssItem(local,    android.R.layout.simple_list_item_1, result);

            // Set list adapter for the ListView
            canItems.setAdapter (adapter);

            // Set list view item click listener
            canItems.setOnItemClickListener (new ListListener (result, local));
        }
    }
}

以下是我对活动的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


<ProgressBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:indeterminate="false"
    android:max="12"
    android:padding="10dp"/>

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/recallList"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"/>


</LinearLayout>

我的Logcat:

java.lang.NullPointerException
        at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
        at android.widget.ListView.setAdapter(ListView.java:480)
        at com.customerautomotivenetwork.VehicleRecall$GetRSSDataTask.onPostExecute(VehicleRecall.java:70)
        at com.customerautomotivenetwork.VehicleRecall$GetRSSDataTask.onPostExecute(VehicleRecall.java:39)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

有关此事的任何帮助吗?

1 个答案:

答案 0 :(得分:0)

在onPostExecute方法中,您必须检查result == null的情况。当有异常时,您当前的doInBackground方法返回null。 rssReader.getItem()也可能返回null。