package com.bobo;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;public class ItemActivity extends Activity {
private ListView listView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
          listView = (ListView)findViewById(R.id.chat1);
 ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String,Object>>();
//  for (int i = 0; i < 5; i++) {
 HashMap<String, Object> map = new HashMap<String, Object>();
 map.put("name", "name");
 map.put("time", "time" );
 map.put("message", "message");
 listItem.add(map);
// }
 SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItem,
                                 R.layout.chat_item,
                       new String[]{"name","time","message" },
                       new int[]{R.id.show_name,R.id.show_time,R.id.message}
                               );
 listView.setAdapter(simpleAdapter);
    }
}以上是activity的代码
下面是listview的chat.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:id="@+id/chat1"
          />
</LinearLayout>然后是自定义布局chat_item.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
       android:padding="6dip" >
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        
        <TextView android:id="@+id/show_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="asassa"            />
        
        <TextView android:id="@+id/show_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
              android:text="asassa"
            />
        
    </LinearLayout>
    <TextView android:id="@+id/message"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:text="asassa"
    /></LinearLayout>不知道哪里错了大神求帮助

解决方案 »

  1.   

    没贴异常啊
    你说的空指针应该是adapter的吧,参考下面的代码试试protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.home_activity);  
            listView=(ListView) findViewById(R.id.listview);  
            Map<String, String> map = new HashMap<String, String>();  
            map.put("nick","张三");  
            map.put("origtext","张三发了一条微博,哈哈");  
            data.add(map);  
            Map<String, String> map2 = new HashMap<String, String>();  
            map2.put("nick","李四");  
            map2.put("origtext", "李四发了一条微博,哈哈");  
            data.add(map2);  
            SimpleAdapter adapter=new SimpleAdapter(  
                    this,  
                    data, //数据  
                    R.layout.listview,  //listview的布局文件  
                    new String[]{"nick","origtext"},  //填充的数据的key  
                    new int[]{R.id.tv_nick,R.id.tv_origtext}  //填充对象的id  
            );  
            listView.setAdapter(adapter);  
        }  
      

  2.   

    下面是错误09-24 08:17:24.886: D/AndroidRuntime(418): Shutting down VM
    09-24 08:17:24.886: W/dalvikvm(418): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    09-24 08:17:24.906: E/AndroidRuntime(418): FATAL EXCEPTION: main
    09-24 08:17:24.906: E/AndroidRuntime(418): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bobo/com.bobo.ItemActivity}: java.lang.NullPointerException
    09-24 08:17:24.906: E/AndroidRuntime(418):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
    09-24 08:17:24.906: E/AndroidRuntime(418):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
    09-24 08:17:24.906: E/AndroidRuntime(418):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    09-24 08:17:24.906: E/AndroidRuntime(418):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    09-24 08:17:24.906: E/AndroidRuntime(418):  at android.os.Handler.dispatchMessage(Handler.java:99)
    09-24 08:17:24.906: E/AndroidRuntime(418):  at android.os.Looper.loop(Looper.java:123)
    09-24 08:17:24.906: E/AndroidRuntime(418):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    09-24 08:17:24.906: E/AndroidRuntime(418):  at java.lang.reflect.Method.invokeNative(Native Method)
    09-24 08:17:24.906: E/AndroidRuntime(418):  at java.lang.reflect.Method.invoke(Method.java:507)
    09-24 08:17:24.906: E/AndroidRuntime(418):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    09-24 08:17:24.906: E/AndroidRuntime(418):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    09-24 08:17:24.906: E/AndroidRuntime(418):  at dalvik.system.NativeStart.main(Native Method)
    09-24 08:17:24.906: E/AndroidRuntime(418): Caused by: java.lang.NullPointerException
    09-24 08:17:24.906: E/AndroidRuntime(418):  at com.bobo.ItemActivity.onCreate(ItemActivity.java:31)
    09-24 08:17:24.906: E/AndroidRuntime(418):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    09-24 08:17:24.906: E/AndroidRuntime(418):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    09-24 08:17:24.906: E/AndroidRuntime(418):  ... 11 more
      

  3.   

    (ItemActivity.java:31)31行是什么东西?
      

  4.   

    你确认一下 R.layout.chat_item  这个xml里面是否包含R.id.show_name,R.id.show_time,R.id.message 这三个ID
      

  5.   

    你代码里面是  setContentView(R.layout.main);下面你说xml是叫  chat.xml是不是名字搞错了?
      

  6.   

    同意楼上,setContentView(R.layout.main);这个有问题
      

  7.   

    setContentView(R.layout.main); 应该是setContentView(R.layout.chat);
      

  8.   

    long playlistid = Long.parseLong(cursor.getString(mIdIdx));
    uri = MediaStore.Audio.Playlists.Members.getContentUri(
    "external", playlistid);
    ret = MusicUtils.query(mActivity, uri, mPlaylistMemberCols,
    where.toString(), null, mSortOrder);
    } //
    int songnum = ret.getCount();N久了,现在感觉在bindview里面常出现null指针异常。怎么办?
    像上面代码中int songnum = ret.getCount(); 这行。求教啊!!
      

  9.   

    AndroidRuntime(418): Caused by: java.lang.NullPointerException 09-24 08:17:24.906: E/AndroidRuntime(418):     at com.bobo.ItemActivity.onCreate(ItemActivity.java:31) 根据这两句话,你看看你写的程序中的第31行是不是有问题