在logcat成功但空屏幕

时间:2013-04-14 14:48:38

标签: android android-layout

我目前有这个问题。因此,我不能做任何其他因为我必须参考这个页面。如果有人能指出我犯的错误,我将不胜感激。

我想查看特定产品的详细信息,其中上一页(列表)中的按钮会将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)

new 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

非常感谢你!

1 个答案:

答案 0 :(得分:2)

这缺少一些代码,所以这个答案中的一些是在黑暗中拍摄的,但你应该在这里采取的关键步骤是:

  • 您已经为您的活动和适配器充气了xml文件(我假设正在使用自定义适配器)
  • 您已使用ID
  • 从视图中选择了列表视图
  • 您已设置listview的适配器
  • 你已经设置了onClick监听器(我可以看到你已经完成了。)
  • 您已覆盖适配器的getView以正确设置数据

如果您已完成这5项工作,那么一旦获得JSON数据,您在做什么?您应该告诉活动处理数据,这也应该在处理程序中完成,因为您使用异步任务执行此操作。

还要确保以某种方式将数据添加到适配器。

最后,这是一个关于listViews的好教程,其中包含自定义列表适配器,可能有一些代码可以帮助您发现错误(它看起来不像我们可以看到的任何代码)http://www.ezzylearning.com/tutorial.aspx?tid=1763429