2013-11-23 107 views
-1

搜索結果的時候,當我的應用程序嘗試顯示搜索結果的應用程序被關閉,應用程序被關閉是這樣的:http://i.imgur.com/YPl4Bfw.jpg?1安卓:顯示ListView中

這裏是logcat的信息顯示

E/Buffer Error(784):轉換結果java.lang.NullPointerException的錯誤:lock == null

E/JSON解析器(784):解析數據時出錯org.json.JSONException:字符0處的輸入結束

W/dalvikvm(784):線程ID = 15:螺紋與未捕獲的異常退出(組= 0x40a71930)

E/AndroidRuntime(784):致命異常:的AsyncTask#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):at 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):at 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_emulation(784):eglSurfaceAttrib沒有實現

E /窗口管理(784 ):Activity com.example.qrandrary.SearchResultDisplay泄露了窗口com.android.internal.policy.impl.PhoneWindow $ DecorView {40dec338 VE .... R ......D 0,0-470,144}這是最初添加在這裏

E/WindowManager(784):android.view.WindowLeaked:Activity com.example.qrandrary.SearchResultDisplay已泄露窗口com.android.internal.policy.impl。 PhoneWindow $ DecorView {40dec338 VE .... R ...... D 0,0-470,144}原來在這裏添加的

E/WindowManager(784):at android.view.ViewRootImpl。(ViewRootImpl。的java:354)

E /窗口管理(784):在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)

E /窗口管理(784):在android.view.WindowMana gerImpl.addView(WindowManagerImpl.java:69)

E /窗口管理(784):在android.app.Dialog.show(Dialog.java:281)

E /窗口管理(784):在COM。 example.qrandrary.SearchResultDisplay $ LoadAllBooks.onPreExecute(SearchResultDisplay.java:68)

E /窗口管理器(784):在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)

E /窗口管理器( 784):at android.os.AsyncTask.execute(AsyncTask.java:534)

E/WindowManager(784):at com.example.qrandrary.SearchResultDisplay.onCreate(SearchResultDisplay.java:53)

E/WindowManager(784):at android.app.Activity.performCreate(Activity.java:5104)

E /窗口管理(784):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

E /窗口管理(784):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2144)

E/WindowManager(784):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

E /窗口管理器(784):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)

E /窗口管理器(784):在android.app.ActivityThread $ H.handleMessage(ActivityThread的.java:1234)

E /窗口管理(784):在android.os.Handler.dispatchMessage(Handler.java:99)

E /窗口管理(784):在android.os.Looper.loop (Looper.java:137)

E/WindowManager(784):at ndroid.app.ActivityThread.main(A ctivityThread.java:5041)

E /窗口管理(784):在java.lang.reflect.Method.invokeNative(本機方法)

E /窗口管理(784):在java.lang.reflect.Method中.invoke(Method.java:511)

E /窗口管理器(784):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)

E /窗口管理器(784 ):在com.android.internal.os.ZygoteInit.main(ZygoteInit。Java的:560)

E /窗口管理器(784):在dalvik.system.NativeStart.main(本機方法)

這裏是代碼:

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; 

    } 
} 

中有什麼問題我的應用程序?

回答

0

我想在JSON中接收你的應用程序有什麼問題。您可以檢查日誌以找出JSON對象中不正確的內容。