我正在通过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