0

我正在通过http客户端解析数据并使用自定义适配器解析列表中的数据。数据存储在数组中,但是在运行时出现异常并且列表不可见。我的代码是

list_item.xml

<?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="horizontal"
    android:weightSum="100" >
<TextView
        android:id="@+id/Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#CC0033"

        />


</LinearLayout>

activity_main.xml

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

    <ListView
        android:id="@+id/li"
        android:layout_height="match_parent" 
        android:layout_width="match_parent"
        ></ListView>

</RelativeLayout>

MainActivity.java

    package com.example.httpwebservices;


public class MainActivity extends Activity {

    ListView list;
    String url,Responce;
    Data data;
    ArrayList<Data> arrayUser;
    ArrayList<String> arrayofString;
    CustomAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

                        url=("http://japp.b4live.com/lectureme.asmx/GetIndividualUser");
                        arrayUser = new ArrayList<Data>();
                        new WebService().execute(url);
                        ListView list=(ListView)findViewById(R.id.li);                      




    }


    public class WebService extends AsyncTask<String, Void, Void>
    {

                @Override
                protected Void doInBackground(String...Arg0) {
                    // TODO Auto-generated method stub

                try {
                        HttpClient client= new DefaultHttpClient();
                        HttpPost request=new HttpPost();
                        request.setURI(new URI(url));
                        List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();

                        nameValuePairs.add(new BasicNameValuePair("skey","U082620130715346802"));
                        UrlEncodedFormEntity Entity=new UrlEncodedFormEntity(nameValuePairs);
                        request.setEntity(Entity);
                        HttpResponse responce=client.execute(request);
                        HttpEntity resEntity=responce.getEntity();

                        if(resEntity!=null)
                        {
                            Responce=EntityUtils.toString(resEntity);
                            //Log.i("Responce",""+Responce);
                            parse(Responce);
                        }
                        else 
                        {
                            Log.i("Responce",""+Responce);
                        }
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (URISyntaxException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


                    return null;
                }

                @Override
                protected void onPostExecute(Void result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                arrayofString=new ArrayList<String>();                  
                for(int i=0 ; i< arrayUser.size() ;i++)
                {
                    data=arrayUser.get(i);
                    arrayofString.add(data.getName());
                    Log.i("parsed", ""+data.getName());
                }   
                CustomAdapter adapter = new CustomAdapter(MainActivity.this,R.layout.list_item,R.id.Name, arrayofString);

                list.setAdapter(adapter);   

                Log.i("data parsed", ""+arrayofString.size());
                }



    }


    public void parse(String xml)
    {



        arrayUser=new ArrayList<Data>();
        Data d=null;

        try {

            boolean name=false,IndividualUser=false,id=false,initial=false,organisation=false,country=false,state=false,city=false,pincode=false,datecreated=false,datemodified=false,usertype=false,photol=false,following=false,followdt=false;

            XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
            XmlPullParser pullParser=factory.newPullParser();
            pullParser.setInput(new StringReader(xml));
            arrayUser=new ArrayList<Data>();
            int eventType=pullParser.getEventType();

            while(eventType != XmlPullParser.END_DOCUMENT)
            {
                switch(eventType)
                {
                case XmlPullParser.START_TAG:
                    if(pullParser.getName().equalsIgnoreCase("GetIndividualUser"))
                    {
                        IndividualUser=true;
                        d=new Data();
                    }
                    if(pullParser.getName().equalsIgnoreCase("mbname"))
                    {
                        name=true;
                    }

                    if(pullParser.getName().equalsIgnoreCase("id"))
                    {
                        id=true;
                    }

                    if(pullParser.getName().equalsIgnoreCase("Initial"))
                    {
                        initial=true;
                    }

                    if(pullParser.getName().equalsIgnoreCase("organization"))
                    {
                        organisation=true;
                    }

                    if(pullParser.getName().equalsIgnoreCase("country"))
                    {
                        country=true;
                    }

                    if(pullParser.getName().equalsIgnoreCase("state"))
                    {
                        state=true;
                    }

                    if(pullParser.getName().equalsIgnoreCase("city"))
                    {
                        city=true;
                    }

                    if(pullParser.getName().equalsIgnoreCase("pincode"))
                    {
                        pincode=true;
                    }

                    if(pullParser.getName().equalsIgnoreCase("datecreated"))
                    {
                        datecreated=true;
                    }

                    if(pullParser.getName().equalsIgnoreCase("datemodified"))
                    {
                        datemodified=true;
                    }

                    if(pullParser.getName().equalsIgnoreCase("usertype_nm"))
                    {
                        usertype=true;
                    }

                    if(pullParser.getName().equalsIgnoreCase("photo1"))
                    {
                        photol=true;
                    }

                    if(pullParser.getName().equalsIgnoreCase("followflag"))
                    {
                        following=true;
                    }

                    if(pullParser.getName().equalsIgnoreCase("followdt"))
                    {
                        followdt=true;
                    }
                    break;
                case XmlPullParser.TEXT:
                    if(name)
                    {
                        d.setName(pullParser.getText().trim());

                    }
                    if(city)
                    {
                        d.setCity(pullParser.getText().trim());

                    }
                    if(country)
                    {
                        d.setCountry(pullParser.getText().trim());

                    }
                    if(datecreated)
                    {
                        d.setDateCreated(pullParser.getText().trim());

                    }
                    if(datemodified)
                    {
                        d.setDateModified(pullParser.getText().trim());

                    }
                    if(followdt)
                    {
                        d.setFollowDate(pullParser.getText().trim());

                    }
                    if(following)
                    {
                        d.setFollowing(pullParser.getText().trim());

                    }
                    if(id)
                    {
                        d.setId(pullParser.getText().trim());

                    }
                    if(IndividualUser)
                    {
                        d.setindividualUser(pullParser.getText().trim());

                    }
                    if(initial)
                    {
                        d.setInitial(pullParser.getText().trim());

                    }
                    if(organisation)
                    {
                        d.setOrganisation(pullParser.getText().trim());

                    }
                    if(photol)
                    {
                        d.setPhoto(pullParser.getText().trim());

                    }
                    if(pincode)
                    {
                        d.setPincode(pullParser.getText().trim());

                    }
                    if(state)
                    {
                        d.setState(pullParser.getText().trim());

                    }
                    if(usertype)
                    {
                        d.setUserType(pullParser.getText().trim());

                    }
                    break;
                case XmlPullParser.END_TAG:
                    if(pullParser.getName().equalsIgnoreCase("GetIndividualUser"))
                    {
                        IndividualUser=false;
                        arrayUser.add(d);
                    }
                    if(pullParser.getName().equalsIgnoreCase("mbname"))
                    {
                        name=false;

                    }
                    if(pullParser.getName().equalsIgnoreCase("id"))
                    {
                        id=false;
                    }

                    if(pullParser.getName().equalsIgnoreCase("Initial"))
                    {
                        initial=false;
                    }

                    if(pullParser.getName().equalsIgnoreCase("organization"))
                    {
                        organisation=false;
                    }

                    if(pullParser.getName().equalsIgnoreCase("country"))
                    {
                        country=false;
                    }

                    if(pullParser.getName().equalsIgnoreCase("state"))
                    {
                        state=false;
                    }

                    if(pullParser.getName().equalsIgnoreCase("city"))
                    {
                        city=false;
                    }

                    if(pullParser.getName().equalsIgnoreCase("pincode"))
                    {
                        pincode=false;
                    }

                    if(pullParser.getName().equalsIgnoreCase("datecreated"))
                    {
                        datecreated=false;
                    }

                    if(pullParser.getName().equalsIgnoreCase("datemodified"))
                    {
                        datemodified=false;
                    }

                    if(pullParser.getName().equalsIgnoreCase("usertype_nm"))
                    {
                        usertype=false;
                    }

                    if(pullParser.getName().equalsIgnoreCase("photo1"))
                    {
                        photol=false;
                    }

                    if(pullParser.getName().equalsIgnoreCase("followflag"))
                    {
                        following=false;
                    }

                    if(pullParser.getName().equalsIgnoreCase("followdt"))
                    {
                        followdt=false;
                    }
                    break;
                default:
                    break;
                }
                eventType=pullParser.next();
                }
            //Log.i("count", ""+ arrayUser.size());



        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }





    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

CUstomAdapter.java

public class CustomAdapter extends ArrayAdapter<Data>{

     Context context;


    public CustomAdapter(Context context, int textViewResourceId,
            int text1, ArrayList<String> arrayofString) {
        super(context, textViewResourceId);
        this.context=context;
        // TODO Auto-generated constructor stub
    }

    private class ViewHolder {
        TextView name;

    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        Data rowItem = getItem(position);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.Name);

            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.name.setText(rowItem.getName());

        return convertView;
    }
}

数据.java

public class Data {

    String IndividualUser,name,id,initial,organisation,country,state,city,pincode,datecreated,datemodified,usertype,photol,following,followdt;

    public String getindividualUser() {
        return IndividualUser;
    }

    public void setindividualUser(String IndividualUser) {
        this.IndividualUser = IndividualUser;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }}

日志猫,,

09-20 14:21:28.127: E/AndroidRuntime(968): FATAL EXCEPTION: main
09-20 14:21:28.127: E/AndroidRuntime(968): java.lang.NullPointerException
09-20 14:21:28.127: E/AndroidRuntime(968):  at com.example.httpwebservices.MainActivity$WebService.onPostExecute(MainActivity.java:119)
09-20 14:21:28.127: E/AndroidRuntime(968):  at com.example.httpwebservices.MainActivity$WebService.onPostExecute(MainActivity.java:1)
09-20 14:21:28.127: E/AndroidRuntime(968):  at android.os.AsyncTask.finish(AsyncTask.java:631)
09-20 14:21:28.127: E/AndroidRuntime(968):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-20 14:21:28.127: E/AndroidRuntime(968):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
09-20 14:21:28.127: E/AndroidRuntime(968):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 14:21:28.127: E/AndroidRuntime(968):  at android.os.Looper.loop(Looper.java:137)
09-20 14:21:28.127: E/AndroidRuntime(968):  at android.app.ActivityThread.main(ActivityThread.java:5041)
09-20 14:21:28.127: E/AndroidRuntime(968):  at java.lang.reflect.Method.invokeNative(Native Method)
09-20 14:21:28.127: E/AndroidRuntime(968):  at java.lang.reflect.Method.invoke(Method.java:511)
09-20 14:21:28.127: E/AndroidRuntime(968):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-20 14:21:28.127: E/AndroidRuntime(968):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-20 14:21:28.127: E/AndroidRuntime(968):  at dalvik.system.NativeStart.main(Native Method)
09-20 14:21:43.137: I/Process(968): Sending signal. PID: 968 SIG: 9
4

3 回答 3

0

如果list.setAdapter(adapter);导致null pointer exception,这可能表明list对象为空。

list=(ListView)findViewById(R.id.li);

这表明您的content view R.layout.activity_main不包含List view R.id.li.

重新检查您是否activity_main.xmlList view R.id.li

于 2013-09-20T13:19:48.730 回答
0
for(int i=0;i<arrayUser.size();i++)

arrayUser为空,你得到 NPE。

你很可能认为你初始化了它WebService.parse()但是

ArrayList<Data> arrayUser=null;
arrayUser=new ArrayList<Data>();

您正在使用本地实例对其进行遮蔽。

通过在 parse() 方法中删除此行来尝试

ArrayList<Data> arrayUser=null;
于 2013-09-20T12:47:50.313 回答
0

你有这个

  ArrayList<Data> arrayUser; // as a activity class member

然后在 onCreate

  for(int i=0;i<arrayUser.size();i++) // arrayUser is null i guess.

也在解析中

  public void parse(String xml)
  {
    ArrayList<Data> arrayUser=null;  // local to parse method. delete this
    arrayUser=new ArrayList<Data>();   

将下面的内容移到内部onPostExecute,这样一旦 doInBackground 执行并填充了您的列表,您就可以将参数传递给 CustomAdapter 并更新列表视图。

  for(int i=0;i<arrayUser.size();i++)
            {
                data=arrayUser.get(i);
                arrayofString.add(data.getName());
                //Log.i("parsed", ""+data.getName());
            }                               
            CustomAdapter adapter = new CustomAdapter(getApplicationContext(),R.layout.list_item,android.R.id.text1, arrayofString);
            list.setAdapter(adapter);  

编辑:

你有这个

 public class MainActivity extends Activity {

  ListView list;

然后在onCreate你有

  ListView list=(ListView)findViewById(R.id.li); // list becomes local to onCreate

所以改变这个

    ListView list=(ListView)findViewById(R.id.li); 

    list=(ListView)findViewById(R.id.li);  
于 2013-09-20T12:48:43.630 回答