列表视图未显示:如何解决此问题

时间:2014-08-05 04:36:04

标签: android listview android-asynctask

我已经完成了解析doInBackground。并得到了结果。但是当我添加到SimpleAdapter时它会显示异常。我怎么解决这个问题?

此处代码是:

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                // Dismiss the progress dialog
                if (pDialog.isShowing())
                    pDialog.dismiss();

                ListAdapter adapter = new SimpleAdapter(
                        HomeActivity.this, contactList,
                        R.layout.list_item, new String[] {TAG_FNAME}, new int[] {R.id.textView1});
                lv.setAdapter(adapter);
                lv.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent,
                            View view, int position, long ids) {
                        Intent i = new Intent(getApplicationContext(),
                                SingleListItem.class);
                        i.putExtra("id", id);
                        i.putExtra("firstName", firstName);
                        i.putExtra("lastName", lastName);
                        i.putExtra("headline", headline);
                        i.putExtra("pictureUrl", pictureUrl);
                        i.putExtra("url", url);
                        startActivity(i);
                    }
                });


            }

并在 Logcat

 java.lang.NullPointerException
    at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
    at android.widget.ListView.setAdapter(ListView.java:460)
    at com.android.linkedin.HomeActivity$2$GetContacts.onPostExecute(HomeActivity.java:204)
    at com.android.linkedin.HomeActivity$2$GetContacts.onPostExecute(HomeActivity.java:1)
    at android.os.AsyncTask.finish(AsyncTask.java:602)
    at android.os.AsyncTask.access$600(AsyncTask.java:156)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4441)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)

Profile_activity.xml:

        <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/blue_gradient_header"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="32dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Linkedin"
        android:textColor="@android:color/white"
        android:textSize="22sp"
        android:textStyle="bold"
        android:typeface="sans" >
    </TextView>
</RelativeLayout>

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/view"
    android:layout_below="@+id/header"
    android:padding="15dp"
    android:textColor="@android:color/white"
    android:textSize="16sp"
    android:textStyle="bold"
    />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</LinearLayout>

<Button
    android:id="@+id/view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="93dp"
    android:background="@drawable/images"
    android:visibility="visible" />

<Button
    android:id="@+id/share"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/view"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="31dp"
    android:background="@drawable/linkedin_share"
    android:visibility="visible" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/name" >
</ListView>

</RelativeLayout>

布局文件有什么问题吗?

1 个答案:

答案 0 :(得分:1)

确保您的 onCreate()方法中包含此行:

lv = (ListView) findViewById(R.id.your_id); //Replace "your_id" with your ListView id

同样在 onPostExecute()方法中,使用以下内容:

adapter.notifyDataSetChanged();//This will change the adapter content