android:在listView中显示搜索结果时,应用程序已关闭
当我的应用程序尝试显示搜索结果时,应用程序关闭如下: 这是logcat中显示的信息 E/缓冲区错误(784):转换结果java.lang.NullPointerException:lock==null时出错 E/JSON解析器(784):解析数据org.JSON.JSONException时出错:输入结束于的字符0处 W/dalvikvm(784):线程ID=15:线程退出时出现未捕获异常(组=0x40a71930) E/AndroidRuntime(784):致命异常:异步任务4 E/AndroidRuntime(784):java.lang.RuntimeException:执行doInBackground()时出错 E/AndroidRuntime(784):位于android.os.AsyncTask$3.done(AsyncTask.java:299) E/AndroidRuntime(784):位于java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) E/AndroidRuntime(784):在 java.util.concurrent.FutureTask.setException(FutureTask.java:219) E/AndroidRuntime(784):在 java.util.concurrent.FutureTask.run(FutureTask.java:239) E/AndroidRuntime(784):位于android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) E/AndroidRuntime(784):位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) E/AndroidRuntime(784):位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) E/AndroidRuntime(784):位于java.lang.Thread.run(Thread.java:856) E/AndroidRuntime(784):由以下原因引起:java.lang.NullPointerException E/AndroidRuntime(784):在com.example.qrandrary.SearchResultDisplay$LoadAllBooks.doInBackground(SearchResultDisplay.java:82) E/AndroidRuntime(784):在com.example.qrandrary.SearchResultDisplay$LoadAllBooks.doInBackground(SearchResultDisplay.java:1) E/AndroidRuntime(784):位于android.os.AsyncTask$2.call(AsyncTask.java:287) E/AndroidRuntime(784):位于java.util.concurrent.FutureTask.run(FutureTask.java:234) E/AndroidRuntime(784):。。。4更多 11-23 14:24:49.267:W/EGL_仿真(784):未实现eglSurfaceAttrib E/WindowManager(784):Activity com.example.qrandrary.SearchResultDisplay泄漏了最初添加到此处的window com.android.internal.policy.impl.PhoneWindow$DecorView{40dec338 V.E…..R…D 0,0-470144} E/WindowManager(784):android.view.WindowLeaked:Activity com.example.qrandrary.SearchResultDisplay泄漏了最初添加到此处的window com.android.internal.policy.impl.PhoneWindow$DecorView{40dec338 V.E…..R….D 0,0-470144} E/WindowManager(784):位于android.view.ViewRootImpl(ViewRootImpl.java:354) E/WindowManager(784):位于android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216) E/WindowManager(784):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) E/WindowManager(784):位于android.app.Dialog.show(Dialog.java:281) E/WindowManager(784):位于com.example.qrandrary.SearchResultDisplay$LoadAllBooks.onPreExecute(SearchResultDisplay.java:68) E/WindowManager(784):位于android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) E/WindowManager(784):位于android.os.AsyncTask.execute(AsyncTask.java:534) E/WindowManager(784):位于com.example.qrandrary.SearchResultDisplay.onCreate(SearchResultDisplay.java:53) E/WindowManager(784):位于android.app.Activity.performCreate(Activity.java:5104) E/WindowManager(784):位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) E/WindowManager(784):位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) E/WindowManager(784):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) E/WindowManager(784):在android.app.ActivityThread.access$600(ActivityThread.java:141) E/WindowManager(784):位于android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) E/WindowManager(784):位于android.os.Handler.dispatchMessage(Handler.java:99) E/WindowManager(784):位于android.os.Looper.loop(Looper.java:137) E/WindowManager(784):在 ndroid.app.ActivityThread.main(ActivityThread.java:5041) E/WindowManager(784):位于java.lang.reflect.Method.Invokenactive(本机方法) E/WindowManager(784):位于java.lang.reflect.Method.invoke(Method.java:511) E/WindowManager(784):位于com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) E/WindowManager(784):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) E/WindowManager(784):位于dalvik.system.NativeStart.main(本机方法) 代码如下:android:在listView中显示搜索结果时,应用程序已关闭,android,android-asynctask,Android,Android Asynctask,当我的应用程序尝试显示搜索结果时,应用程序关闭如下: 这是logcat中显示的信息 E/缓冲区错误(784):转换结果java.lang.NullPointerException:lock==null时出错 E/JSON解析器(784):解析数据org.JSON.JSONException时出错:输入结束于的字符0处 W/dalvikvm(784):线程ID=15:线程退出时出现未捕获异常(组=0x40a71930) E/AndroidRuntime(784):致命异常:异步任务4 E/Andr
public class SearchResultDisplay extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> booksList;
// url to get all products list
private static String url_all_books = "http://127.0.0.1/qrandrary/quickSearch.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_BOOKS = "books";
private static final String TAG_BID = "bid";
private static final String TAG_BNAME = "bname";
private static final String TAG_AUTHORS = "authors";
private static final String TAG_STATUS = "status";
private static final String TAG_PUBLISHER = "publisher";
ListView listView;
List<RowItem> rowItems;
// products JSONArray
JSONArray books = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_resultlist_view);
rowItems = new ArrayList<RowItem>();
listView = (ListView) findViewById(android.R.id.list);
new LoadAllBooks().execute();
}
class LoadAllBooks extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SearchResultDisplay.this);
pDialog.setMessage("Loading Books. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
Bundle extras = getIntent().getExtras();
String keyword = extras.getString("keyword");
url_all_books = url_all_books + "?keyword=" + keyword;
JSONObject json = jParser.makeHttpRequest(url_all_books, "GET",
params);
// Check your log cat for JSON reponse
Log.d("All Books: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
books = json.getJSONArray(TAG_BOOKS);
// looping through All Products
for (int i = 0; i < books.length(); i++) {
JSONObject c = books.getJSONObject(i);
// Storing each json item in variable
String bid = c.getString(TAG_BID);
String bName = c.getString(TAG_BNAME);
String authors = c.getString(TAG_AUTHORS);
String publisher = c.getString(TAG_PUBLISHER);
String status = c.getString(TAG_STATUS);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_BID, bid);
map.put(TAG_BNAME, bName);
map.put(TAG_AUTHORS, authors);
map.put(TAG_PUBLISHER, publisher);
map.put(TAG_STATUS, status);
// adding HashList to ArrayList
booksList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
BookListViewAdapter adapter = new BookListViewAdapter(SearchResultDisplay.this,R.layout.search_result_item, rowItems);
listView.setAdapter(adapter);
}
});
}
}
}
public class BookListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
public BookListViewAdapter(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
TextView txtBid;
TextView txtBname;
TextView txtAuthors;
TextView txtPublisher;
TextView txtStatus;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.search_result_item, null);
holder = new ViewHolder();
holder.txtBid = (TextView) convertView.findViewById(R.id.bid);
holder.txtBname = (TextView) convertView.findViewById(R.id.bName);
holder.txtAuthors = (TextView) convertView.findViewById(R.id.authors);
holder.txtPublisher = (TextView) convertView.findViewById(R.id.publisher);
holder.txtStatus = (TextView) convertView.findViewById(R.id.status);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtBid.setText(rowItem.getbId());
holder.txtBname.setText(rowItem.getbName());
holder.txtAuthors.setText(rowItem.getAuthors());
holder.txtPublisher.setText(rowItem.getPublisher());
holder.txtStatus.setText(rowItem.getStatus());
return convertView;
}
}
public class RowItem {
private int bId;
private String bName;
private String authors;
private String status;
private String publisher;
public RowItem(int bId, String bName, String authors, String status,
String publisher) {
this.bId = bId;
this.bName = bName;
this.authors = authors;
this.status = status;
this.publisher = publisher;
}
public int getbId() {
return bId;
}
public void setbId(int bId) {
this.bId = bId;
}
public String getbName() {
return bName;
}
public void setbName(String bName) {
this.bName = bName;
}
public String getAuthors() {
return authors;
}
public void setAuthors(String authors) {
this.authors = authors;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
}
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
公共类SearchResultDisplay扩展了ListActivity{
//进度对话框
私人对话;
//创建JSON解析器对象
JSONParser jParser=新的JSONParser();
ArrayList图书列表;
//获取所有产品列表的url
私有静态字符串url\u所有\u书籍=”http://127.0.0.1/qrandrary/quickSearch.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_BOOKS=“BOOKS”;
私有静态最终字符串标记_BID=“BID”;
私有静态最终字符串标记_BNAME=“BNAME”;
私有静态最终字符串标记_AUTHORS=“AUTHORS”;
私有静态最终字符串标记_STATUS=“STATUS”;
私有静态最终字符串标记\u PUBLISHER=“PUBLISHER”;
列表视图列表视图;
列出项目;
//产品JSONArray
JSONArray books=null;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.search\u resultlist\u视图);
rowItems=新的ArrayList();
listView=(listView)findViewById(android.R.id.list);
新建LoadAllBooks().exe