Android 具有AsyncTask的Listview中的NullPointerException

Android 具有AsyncTask的Listview中的NullPointerException,android,listview,android-asynctask,nullpointerexception,Android,Listview,Android Asynctask,Nullpointerexception,我正在编写一个模块,在后台从服务器获取一些内容并显示在listview上。我的代码是 public class ContentList extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState);

我正在编写一个模块,在后台从服务器获取一些内容并显示在listview上。我的代码是

public class ContentList extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        String contentURL = getIntent().getExtras().getString("contentURL);

        new Retreivecontent().execute(contentURL );

    } 

    class Retreivecontent extends AsyncTask<String, Void, List<String>>{

        @Override
        protected List<String> doInBackground(String... urls) {
            Document document = null;
            List<String> content = new ArrayList<String>();
            Log.d("$$$$$$$$$$$$$$$$$", urls[0]);
            try {
                document = Jsoup.connect(urls[0]).get();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Elements allList = document.getElementsByTag("p");
            for (Element element : allList) {
                String poem = element.select("p:has(br)").text();
                if(!poem.equals("")){
                    content.add(poem);
                }

            }

            return content;
        }

        @Override
        protected void onPostExecute(List<String> result) {
            Log.d("$################", result.toString());
            final ListView listView = (ListView) findViewById(R.id.contentList);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(contentList.this, android.R.layout.simple_list_item_1, result);
            listView.setAdapter(adapter);

        }
    }

}
列表视图的xml代码是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

    </ListView>

</RelativeLayout>

我也有同样的问题

问题是,您必须在任务外部和调用它之前分配listview和适配器,以及设置适配器

通常在onActivityCreated中完成后,如果使用片段,则可以调用asynctask并仅调用Adapter.notifyDataSetChanged;在onPostExecute中

在doInBackground中,您直接更新adatper,如前所述,您只调用yourAdapter.notifyDataSetChanged;任务完成后,在onPostExecute中

您缺少活动的setContentView

ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_content); // set layout to activity
    listView = (ListView) findViewById(R.id.list) // id is list
改变这个

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(contentList.this, android.R.layout.simple_list_item_1, result);


发布stacktrace和一些更相关的codeLogCat,以及XML中的ListView代码?正确调试代码并不困难;可能未找到列表,因此其为null,因此listView.setAdapteradapter;抛出NPEI猜测其listView.setAdapteradapter;
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_content); // set layout to activity
    listView = (ListView) findViewById(R.id.list) // id is list
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(contentList.this, android.R.layout.simple_list_item_1, result);
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(ContentList.this, android.R.layout.simple_list_item_1, result);
 // C in caps