Android-setAdapter函数上的空指针
我正在开发一个应用程序,我试图显示一个包含MySQL数据库信息的列表视图。我已经有了一个不同的活动,我使用php从数据库发送和接收数据,我正在做类似的事情。我不确定我遇到的错误是来自适配器类还是来自AsyncTask中的Json解析部分。以下是文件:Android-setAdapter函数上的空指针,android,json,http,Android,Json,Http,我正在开发一个应用程序,我试图显示一个包含MySQL数据库信息的列表视图。我已经有了一个不同的活动,我使用php从数据库发送和接收数据,我正在做类似的事情。我不确定我遇到的错误是来自适配器类还是来自AsyncTask中的Json解析部分。以下是文件: package com.example.xxx.xxx; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; impo
package com.example.xxx.xxx;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
public class ListActivity extends ActionBarActivity {
ListView list;
ArrayList<Places> placeslist;
PlacesAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_row);
placeslist = new ArrayList<Places>();
new JASONTask().execute("http://xxx.xxx.com/getlist.php");
adapter = new PlacesAdapter(getApplicationContext(), R.layout.single_row, placeslist);
list = (ListView)findViewById(R.id.listView);
list.setAdapter(adapter);
}
public class JASONTask extends AsyncTask<String,Void,Boolean>{
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(ListActivity.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line ="";
while ((line = reader.readLine()) != null){
buffer.append(line);
}
String finalJson = buffer.toString();
JSONArray theJson = new JSONArray(finalJson);
for (int i=0; i<theJson.length(); i++){
Places place = new Places();
JSONObject jRealObject = theJson.getJSONObject(i);
place.setPlaces_id(jRealObject.getString("places_id"));
place.setName(jRealObject.getString("name"));
place.setLocation_address(jRealObject.getString("location_address"));
placeslist.add(place);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if(connection != null) {
connection.disconnect();
}
try {
if (reader != null){
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result == false){
//message not parsed
}
}
}
}
而PlacesAdapter类是:
package com.example.xxx.xxx;
/**
* Created by manhols on 10/12/2015.
*/
public class Places {
private String places_id;
private String name;
private String location_address;
public Places(){
}
public String getPlaces_id() {
return places_id;
}
public void setPlaces_id(String places_id) {
this.places_id = places_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation_address() {
return location_address;
}
public void setLocation_address(String location_address) {
this.location_address = location_address;
}
}
package com.example.xxx.xxx;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by manhols on 10/12/2015.
*/
public class PlacesAdapter extends ArrayAdapter<Places>{
ArrayList<Places> placeslist;
int Resource;
Context context;
LayoutInflater vi;
public PlacesAdapter(Context context, int resource, ArrayList<Places> objects) {
super(context, resource, objects);
Resource = resource;
placeslist = objects;
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
vi.inflate(Resource, null);
convertView = vi.inflate(Resource,null);
holder = new ViewHolder();
holder.textView = (TextView)convertView.findViewById(R.id.textView);
holder.textView2 = (TextView)convertView.findViewById(R.id.textView2);
holder.textView5 = (TextView)convertView.findViewById(R.id.textView5);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(placeslist.get(position).getName());
holder.textView2.setText(placeslist.get(position).getPlaces_id());
holder.textView5.setText(placeslist.get(position).getLocation_address());
return convertView;
}
static class ViewHolder {
public TextView textView;
public TextView textView2;
public TextView textView5;
}
}
我将非常感谢任何帮助解决此问题。我发现您的适配器中存在一些错误。请将行充气为convertView=vi.inflateResource,parent,false;并且在解析数据时,仅在try块中返回true。从您的代码中,我可以看出,即使您有异常,您也会返回true,这是错误的。我的问题是setContentView中的@codeMagic前面提到的。多亏了他,我已经解决了这个问题请提供遇到异常时生成的堆栈跟踪。AsyncTask顾名思义是异步的。这意味着它很可能在执行其余代码之前没有完成。您需要在onPostExecute中调用一个函数来处理适配器,或者直接将其放入该方法中,但是,是的,请发布stacktrace。我觉得setContentViewR.layout.single_row;正在扩大ListView中行的布局。它应该扩展实际包含ListView的布局,以便在尝试初始化它时为空list=ListViewfindViewByIdR.id.ListView;谢谢你@codeMagic你是对的!我的布局有问题。我修复了它,现在它没有给我设置适配器错误。然而,我有一个新问题。Json对象看起来是空的。我会用issueNo更新这个线程,如果你不明白的话,我会为它创建一个新的帖子。但您的新问题很可能是由于看到Hello Sri,谢谢您的帮助。我的问题在setContentView中,正如前面提到的@codeMagic。多亏了他,我已经解决了这个问题。再次感谢!太好了,我还没看过那句台词。谢谢你提到。但是使用我说过的返回语句,它会很有帮助