2013-04-14 69 views
2

我目前有這個問題。正因爲如此,我不能做任何其他的,因爲我不得不參考這個頁面。如果有人能指出我犯的錯誤,我將不勝感激。成功在logcat但空屏幕

我想查看某個特定產品的詳細信息,其中上一頁(列表)中的按鈕將ID參數傳遞給頁面(詳細信息)。 Java代碼或PHP代碼中沒有錯誤。我很確定這是因爲Logcat顯示相應的結果(因爲我在所有地方都添加了日誌)。但該頁面在模擬器中是空的。我不明白爲什麼會發生這種情況,因爲佈局與其他頁面一樣。如果你需要的代碼,其如下:

Java代碼: list.java

ListView lv = getListView(); 

    lv.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

      String pid = ((TextView) view.findViewById(R.id.pid)).getText() 
        .toString(); 

      // Starting new intent 
      Intent in = new Intent(getApplicationContext(), 
        details.class); 
      // sending pid to next activity 
      in.putExtra(TAG_PID, pid); 

      // starting new activity and expecting some response back 
      startActivity(in); 
     } 
    }); 

Java代碼:details.java

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_book_details); 


     Intent i = getIntent(); 

     // getting product id (pid) from intent 
     pid = i.getStringExtra(TAG_PID); 

     Log.d("pid is:",pid); 

     new GetProductDetails().execute(); 

    } 

class GetProductDetails extends AsyncTask<String, String, String> { 


     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(Details.this); 
      pDialog.setMessage("Loading. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     protected String doInBackground(String... params) { 


      runOnUiThread(new Runnable() { 
       public void run() { 

        int success; 
        try { 
    List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       params.add(new BasicNameValuePair("pid", pid)); 

JSONObject json = jParser.makeHttpRequest(url_product_details, "GET", params); 


    Log.d("Single Product Details", json.toString()); 


         success = json.getInt(TAG_SUCCESS); 
         if (success == 1) { 
JSONArray productObj = json.getJSONArray(TAG_BOOK); 
     int i = productObj.length(); //line 1 

JSONObject product = productObj.getJSONObject(i); 

String title = "Title : "+ product.getString(TAG_TITLE); 
    String description = product.getString(TAG_DESCRIPTION); 


HashMap<String, String> map = new HashMap<String, String>(); 


map.put(TAG_TITLE, title); 
map.put(TAG_DESCRIPTION, description);        

eventsList.add(map); 

Log.d("Title", title); 
Log.d("Description", description); 
      } 
} 
     protected void onPostExecute(String file_url) { 
      pDialog.dismiss(); 
      runOnUiThread(new Runnable() { 
       public void run() { 

ListAdapter adapter = new SimpleAdapter(
      Details.this, eventsList, 
R.layout.list_item2, new String[] { TAG_PID, TAG_TITLE, TAG_DESCRIPTION}, 
    new int[] { R.id.pid, R.id.title, R.id.description });     
        setListAdapter(adapter); 
       } 
      }); 
     } 
    } 

佈局:details.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ListView 
    android:layout_marginTop="70dp" 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_marginLeft="20dp"/> 
</LinearLayout> 

logcat:

04-15 00:05:28.005: D/All Products:(4345): {"success":1,"books":[{"author":"Agnesh","category":"Fiction","title":"The Immortals of Shiva","pid":"1","price":"150","description":"Life of Shiva","discount":"20%"},{"author":"Chetan Bhagat","category":"Drama","title":"3 Mistakes of My Life","pid":"14","price":"180","description":"Story of a 3 friends and their destiny","discount":"30%"},{"author":"Chetan Bhagat","category":"Comedy","title":"Two States","pid":"15","price":"175","description":"Love Story","discount":"5%"},{"author":"Charles Darwin","category":"Personality Developm","title":"How to win friends","pid":"16","price":"250","description":"Building your confidence","discount":"10%"},{"author":"Paulo Coelho","category":"Science Fiction","title":"Alchemist","pid":"17","price":"300","description":"In search of Gold","discount":"20%"},{"author":"Vivekananda","category":"Social Awareness","title":"Call to the Nation","pid":"18","price":"100","description":"Knowing yourself","discount":"5%"}]} 
04-15 00:05:29.675: D/pid is:(4345): 18 
04-15 00:05:30.995: D/Single Product Details(4345): {"book":[{"author":"Vivekananda","category":"Social Awareness","title":"Call to the Nation","pid":"18","price":"100","description":"Knowing yourself","discount":"5%"}],"success":1} 
04-15 00:05:31.000: W/System.err(4345): org.json.JSONException: Index 1 out of range [0..1) 
04-15 00:05:31.005: W/System.err(4345):  at org.json.JSONArray.get(JSONArray.java:263) 
04-15 00:05:31.005: W/System.err(4345):  at org.json.JSONArray.getJSONObject(JSONArray.java:480) 
04-15 00:05:31.005: W/System.err(4345):  at com.spyraa.store.Details$GetProductDetails$1.run(Details.java:104) 
04-15 00:05:31.005: W/System.err(4345):  at android.os.Handler.handleCallback(Handler.java:587) 
04-15 00:05:31.005: W/System.err(4345):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-15 00:05:31.005: W/System.err(4345):  at android.os.Looper.loop(Looper.java:130) 
04-15 00:05:31.010: W/System.err(4345):  at android.app.ActivityThread.main(ActivityThread.java:3691) 
04-15 00:05:31.010: W/System.err(4345):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-15 00:05:31.010: W/System.err(4345):  at java.lang.reflect.Method.invoke(Method.java:507) 
04-15 00:05:31.010: W/System.err(4345):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) 
04-15 00:05:31.010: W/System.err(4345):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 
04-15 00:05:31.015: W/System.err(4345):  at dalvik.system.NativeStart.main(Native Method) 
04-15 00:05:31.020: D/AndroidRuntime(4345): Shutting down VM 
04-15 00:05:31.020: W/dalvikvm(4345): threadid=1: thread exiting with uncaught exception (group=0x4001e578) 
04-15 00:05:31.030: E/AndroidRuntime(4345): FATAL EXCEPTION: main 
04-15 00:05:31.030: E/AndroidRuntime(4345): java.lang.NullPointerException 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at android.widget.ListView.setAdapter(ListView.java:485) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at android.app.ListActivity.setListAdapter(ListActivity.java:265) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at com.spyraa.store.Details$GetProductDetails$2.run(Details.java:166) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at android.app.Activity.runOnUiThread(Activity.java:3743) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at com.spyraa.store.Details$GetProductDetails.onPostExecute(Details.java:156) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at com.spyraa.store.Details$GetProductDetails.onPostExecute(Details.java:1) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at android.os.AsyncTask.finish(AsyncTask.java:417) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at android.os.AsyncTask.access$300(AsyncTask.java:127) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at android.os.Looper.loop(Looper.java:130) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at android.app.ActivityThread.main(ActivityThread.java:3691) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at java.lang.reflect.Method.invoke(Method.java:507) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 
04-15 00:05:31.030: E/AndroidRuntime(4345):  at dalvik.system.NativeStart.main(Native Method) 

新的logcat:

04-15 01:37:50.695: D/pid is:(8773): 18 
04-15 01:37:52.285: D/Single Product Details(8773): {"book":[{"author":"Vivekananda","category":"Social Awareness","title":"Call to the Nation","pid":"18","price":"100","description":"Knowing yourself","discount":"5%"}],"success":1} 
04-15 01:37:52.295: D/AndroidRuntime(8773): Shutting down VM 
04-15 01:37:52.295: W/dalvikvm(8773): threadid=1: thread exiting with uncaught exception (group=0x4001e578) 
04-15 01:37:52.305: E/AndroidRuntime(8773): FATAL EXCEPTION: main 
04-15 01:37:52.305: E/AndroidRuntime(8773): java.lang.NullPointerException 
04-15 01:37:52.305: E/AndroidRuntime(8773):  at com.spyraa.bookstore.BookDetails$GetProductDetails$1.run(BookDetails.java:127) 
04-15 01:37:52.305: E/AndroidRuntime(8773):  at android.os.Handler.handleCallback(Handler.java:587) 
04-15 01:37:52.305: E/AndroidRuntime(8773):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-15 01:37:52.305: E/AndroidRuntime(8773):  at android.os.Looper.loop(Looper.java:130) 
04-15 01:37:52.305: E/AndroidRuntime(8773):  at android.app.ActivityThread.main(ActivityThread.java:3691) 
04-15 01:37:52.305: E/AndroidRuntime(8773):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-15 01:37:52.305: E/AndroidRuntime(8773):  at java.lang.reflect.Method.invoke(Method.java:507) 
04-15 01:37:52.305: E/AndroidRuntime(8773):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) 
04-15 01:37:52.305: E/AndroidRuntime(8773):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 
04-15 01:37:52.305: E/AndroidRuntime(8773):  at dalvik.system.NativeStart.main(Native Method) 
04-15 01:38:00.015: I/Process(8773): Sending signal. PID: 8773 SIG: 9 

太感謝你了!

+0

莫非你一個很好的教程發佈LogCat的輸出? – thomas88wp

+0

@ thomas88wp,yup,已更新。 –

+0

您使用的是列表適配器嗎? –

回答

2

這是缺少一些代碼,所以一些這個答案是在黑暗中拍攝,但你應該有這裏的關鍵步驟是:

  • 你誇大你的XML文件,這對你的活動和適配器(我假設正在使用自定義適配器)
  • 你從它的觀點拿起你的列表視圖的ID
  • 你已經設置ListView的適配器
  • 你已經設置的onClick監聽器(我可以看到你已經做到了。)
  • 你overrided適配器的getView設置你的數據正確

如果你做這些的5件事,那麼你有什麼用JSON數據做一旦你明白了嗎?您應該告訴活動來處理數據,這也應該在處理程序中完成,因爲您正在使用異步任務執行此操作。

還要確保您以某種方式將數據添加到適配器。

最後,這裏是與自定義列表適配器列表視圖,可能有一些代碼,可以幫助你發現錯誤(它看起來並不像它在任何代碼,我們可以看到)http://www.ezzylearning.com/tutorial.aspx?tid=1763429

+0

我幫你。謝謝。我會盡力在這裏更新。 –

+0

我根據您的評論更新了代碼,logcat看起來像更新的logcat。我知道在詳細信息頁面中有錯誤,我在其中評論//第1行。是否可以讓它正確? –

+0

您正在獲取數組,數組的長度爲i,然後嘗試在位置i處獲取對象。如果我有一個大小爲1的數組,則唯一的對象爲0.嘗試在i-1上獲取對象。這應該解決你的ArrayOutOfRange異常編輯:要進一步指定,它的行JSONObject product = productObj.getJSONObject(i);使該行i-1 –