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>不知道哪里错了大神求帮助
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>不知道哪里错了大神求帮助
解决方案 »
- android assets文件夹下的资源操作
- 小问题,求指正,本人新手
- 如何记录查询哪个引用发出ACTION_PACKAGE_ADDED这个广播
- 如何实现menu底部菜单的四个菜单项MenuItem在一行显示?
- intent 小问题
- 请教几个问题,关于SurfaceView,关于图片。
- android 如何显示一段文本的关键字显示在屏幕中间,而且高亮显示
- android是否支持定时启动一个进程?
- 请教图片叠加效果的实现
- 实现一个分组的ListView但是以滚动就会出现空指针的异常
- RelativeLayout中动态增加imageView,拖动过的控件位置还原到拖动前的位置,求好心人解答下菜鸟问题
- android源代码编译之后,烧入板子中没有声音
你说的空指针应该是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);
}
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
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(); 这行。求教啊!!