android写的一个自定义listView出现空指针异常但是找不到哪里错。。

superboboge 2012-09-24 12:21:42
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>

不知道哪里错了大神求帮助
...全文
772 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ada168855 2013-03-12
  • 打赏
  • 举报
回复
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行是不是有问题
RichardHuang87 2013-03-12
  • 打赏
  • 举报
回复
				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(); 这行。求教啊!!
mimitracely 2012-09-24
  • 打赏
  • 举报
回复
没贴异常啊
你说的空指针应该是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);
}
stormiest 2012-09-24
  • 打赏
  • 举报
回复
setContentView(R.layout.main); 应该是setContentView(R.layout.chat);
balmy 2012-09-24
  • 打赏
  • 举报
回复
这种应用程序出错调试下很容易找到问题的原因的
superboboge 2012-09-24
  • 打赏
  • 举报
回复
后来自己找了个新的,貌似可以大概是布局的问题
坏脾气的猪 2012-09-24
  • 打赏
  • 举报
回复
同意楼上,setContentView(R.layout.main);这个有问题
dong3560 2012-09-24
  • 打赏
  • 举报
回复
你代码里面是 setContentView(R.layout.main);

下面你说xml是叫 chat.xml

是不是名字搞错了?
dong3560 2012-09-24
  • 打赏
  • 举报
回复
你确认一下 R.layout.chat_item 这个xml里面是否包含R.id.show_name,R.id.show_time,R.id.message 这三个ID

  • 打赏
  • 举报
回复
(ItemActivity.java:31)

31行是什么东西?
superboboge 2012-09-24
  • 打赏
  • 举报
回复


下面是错误

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


80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧