listview不工作android not null指针异常

时间:2015-07-23 14:16:08

标签: java android json android-activity

我正在尝试在我的ListView中显示来自网站的数据,但它没有显示数据。我认为我的`AsyncTask

中存在错误

我想只查看我收到的json文件中的两个文本。

帮助我了解我的错误。 我尝试使用ipadress和域名没有任何作用,这不是一个例外

这是我的代码

package com.itdc.shashank.avdeal1;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;


public class itemView extends Activity {
    ArrayList<item> list1;
    actorAdapter adp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_view);
        list1=new ArrayList<item>();
        Log.d("hello", "notworking");
        new JSONAsyncTask().execute("http://avdeal.in/get_all_products.php");


    }

    public class JSONAsyncTask extends AsyncTask<String,Void, Boolean>{
        ProgressDialog pd;
        protected void OnPreexecute(){

            super.onPreExecute();
            pd=new ProgressDialog(itemView.this);
            pd.setMessage("Please Wait");
            pd.setTitle("connecting");
            pd.show();
            pd.setCancelable(false);
        }
        @Override
        protected Boolean doInBackground(String... urls) {
            try {
                HttpGet httppost=new HttpGet(urls[0]);
                HttpClient httpClient=new DefaultHttpClient();
                HttpResponse response=httpClient.execute(httppost);

                int status=response.getStatusLine().getStatusCode();
                if (status>0)
                {
                    HttpEntity entity=response.getEntity();
                    String data= EntityUtils.toString(entity);

                    JSONObject jsono=new JSONObject(data);
                    JSONArray jsonArray=jsono.getJSONArray("item");
                for (int i=0;i<jsonArray.length();i++)
                {
                    JSONObject object=jsonArray.getJSONObject(i);
                    item Items=new item(object.getString("product_title"),object.getString("product_price"));
                   // item.setName(object.getString("product_title"));
                  //  item.setPrice(object.getString("product_price"));
                    list1.add(Items);

                }
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return true;
        }

        protected void onPostExecute(boolean result){
            pd.dismiss();

            adp.notifyDataSetChanged();
            if (result==false)
                Toast.makeText(getApplicationContext(), "Unable to fetch data", Toast.LENGTH_SHORT).show();
            ListView lv= (ListView) findViewById(R.id.list);
            adp=new actorAdapter(getApplicationContext(),R.layout.listview, list1);
            lv.setAdapter(adp);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        return true;
    }

   }

这是我使用webaddress但网站工作时唯一的logcat

07-23 20:37:09.008  13927-14195/com.itdc.shashank.avdeal1 I/System.out﹕ [CDS][DNS]Unable to resolve host "www.avdeal.in": No address associated with hostname
07-23 20:37:09.010  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ java.net.UnknownHostException: Unable to resolve host "www.avdeal.in": No address associated with hostname
07-23 20:37:09.019  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:439)
07-23 20:37:09.019  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-23 20:37:09.020  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-23 20:37:09.020  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-23 20:37:09.020  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-23 20:37:09.020  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-23 20:37:09.021  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
07-23 20:37:09.021  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-23 20:37:09.022  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-23 20:37:09.022  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-23 20:37:09.022  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at com.itdc.shashank.avdeal1.itemView$JSONAsyncTask.doInBackground(itemView.java:57)
07-23 20:37:09.023  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at com.itdc.shashank.avdeal1.itemView$JSONAsyncTask.doInBackground(itemView.java:41)
07-23 20:37:09.024  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-23 20:37:09.024  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-23 20:37:09.025  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-23 20:37:09.025  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-23 20:37:09.026  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-23 20:37:09.026  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.lang.Thread.run(Thread.java:838)
07-23 20:37:09.027  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
07-23 20:37:09.029  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at libcore.io.Posix.getaddrinfo(Native Method)
07-23 20:37:09.029  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:59)
07-23 20:37:09.030  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
07-23 20:37:09.031  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ ... 17 more
07-23 20:37:09.031  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ Caused by: libcore.io.ErrnoException: getaddrinfo failed: ETIMEDOUT (Connection timed out)
07-23 20:37:09.033  13927-14195/com.itdc.shashank.avdeal1 W/System.err﹕ ... 20 more

1 个答案:

答案 0 :(得分:0)

我猜这个问题与您的网址http://108.170.10.163/get_all_products.php有关。检查一下。此网址显示404(找不到网页)错误。

编辑:

根据@Selvin的评论,你也得到一个NUllPointerException

创建适配器后,需要添加此adp.notifyDataSetChanged()。这意味着您需要这样做。

 adp=new actorAdapter(getApplicationContext(),R.layout.listview, list1);
 adp.notifyDataSetChanged();