我正在尝试将一些数据放入listview,但我有这个错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
我认为这是我的适配器需要修复。
感谢您的帮助!
这是我的活动:
package com.example.taypaygay;
import java.util.jar.Attributes.Name;
import com.example.taypaygay.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class Horaires extends Activity implements OnClickListener
{
private String activityName = "Horaires";
private TextView textviewDeparts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horaires);
((Button)findViewById(R.id.buttonretour)).setOnClickListener(this);
ListView listView = (ListView)findViewById(R.id.listView);
textviewDeparts = (TextView)this.findViewById(R.id.departs);
ThreadStopName threadname = new ThreadStopName(listView, this);
threadname.execute(textviewDeparts);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.buttonretour)
{
finish();
}
}
/*private void print(String str)
{
textviewDeparts.setText(textviewDeparts.getText() + "\n" + str);
}*/
}
这是“ThreadStopName”
package com.example.taypaygay;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ThreadStopName extends AsyncTask<TextView, String, String>{
public InputStream stream;
public TextView textView;
public static String Name;
public ListView list2;
private Activity context;
public ThreadStopName(ListView list, Activity context)
{this.list2 = list;
this.context = context;}
public ListView listView;
public void parseXML()
{
try {
URI url=null;
url = new URI(MainActivity.QueryStop);
// open the file
try {
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(new HttpGet(url));
stream = response.getEntity().getContent();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
publishProgress("Exception: " + e.toString());
}
// print(">Input stream");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringComments(true);
dbf.setIgnoringElementContentWhitespace(true);
// NOTE: not valid on Android
//dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
// start parsing the file
Document doc = db.parse(stream);
// normalize the content
doc.getDocumentElement().normalize();
// this.print("Root element " + doc.getDocumentElement().getNodeName());
// get the pointer to all the users
NodeList stopList = doc.getElementsByTagName("stopName");
String Name="";
List<String> contentArray = new ArrayList<String>();
for (int i = 0; i < stopList.getLength(); i++)
{
Element elementStopCode = (Element)stopList.item(i);
String strName = elementStopCode.getTextContent();
contentArray.add(elementStopCode.getTextContent());
Name = Name.concat("\n"+ strName);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, R.layout.simplerow, contentArray);
listView.setAdapter(arrayAdapter);
publishProgress(Name);
/*Node name = userContent.item(1);
Element elName = (Element)name;*/
// get the attribute
//String attribute = user.getAttributes().item(0).getTextContent();
}
catch (Exception e)
{
e.printStackTrace();
publishProgress("Exception: " + e.toString());
//this.print("Exception " + e.toString());
}
}
@Override
protected String doInBackground(TextView... params) {
textView = params[0];
parseXML();
return null;
}
protected void onProgressUpdate(String...p)
{
String message = p[0];
textView.setText(message);
}
}
这是我的“activity_horaires.xml”:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
tools:context="com.example.tpgcapese.Activity2" >
<TextView
android:id="@+id/Horaire"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="Arrêts à 500m"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp"
android:textStyle="bold" />
<Button
android:id="@+id/buttonretour"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="Retour"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/departs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Horaire"
android:layout_alignRight="@+id/Horaire"
android:layout_below="@+id/Horaire"
android:layout_marginTop="22dp"
android:text="TextView" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/departs"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
最后“simplerow.xml”:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
答案 0 :(得分:1)
您需要在setAdapter
list2
public ThreadStopName(ListView list, Activity context) {
this.list2 = list;
this.context = context;
}
listView.setAdapter(arrayAdapter); should be changed