我想在这里提出几点:
listview的每个项目包含一个imageButton,2个TextView和一个imageView
我想在活动中获取itemView的imageButton 位置而不是点击事件,因为我想将badgeView添加到imageButton
错误信息如下:
--------------------错误信息----------------
03-04 07:03:07.226: D/dalvikvm(712): GC_CONCURRENT freed 48K, 3% free 10270K/10503K, paused 8ms+7ms
03-04 07:03:07.816: D/gralloc_goldfish(712): Emulator without GPU emulation detected.
03-04 07:03:17.307: D/AndroidRuntime(712): Shutting down VM
03-04 07:03:17.307: W/dalvikvm(712): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
03-04 07:03:17.366: E/AndroidRuntime(712): FATAL EXCEPTION: main
03-04 07:03:17.366: E/AndroidRuntime(712): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.list_1/com.example.list_1.ClinicMessage}: java.lang.NullPointerException
03-04 07:03:17.366: E/AndroidRuntime(712): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
03-04 07:03:17.366: E/AndroidRuntime(712): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-04 07:03:17.366: E/AndroidRuntime(712): at android.app.ActivityThread.access$600(ActivityThread.java:122)
03-04 07:03:17.366: E/AndroidRuntime(712): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-04 07:03:17.366: E/AndroidRuntime(712): at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 07:03:17.366: E/AndroidRuntime(712): at android.os.Looper.loop(Looper.java:137)
03-04 07:03:17.366: E/AndroidRuntime(712): at android.app.ActivityThread.main(ActivityThread.java:4340)
03-04 07:03:17.366: E/AndroidRuntime(712): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 07:03:17.366: E/AndroidRuntime(712): at java.lang.reflect.Method.invoke(Method.java:511)
03-04 07:03:17.366: E/AndroidRuntime(712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-04 07:03:17.366: E/AndroidRuntime(712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-04 07:03:17.366: E/AndroidRuntime(712): at dalvik.system.NativeStart.main(Native Method)
03-04 07:03:17.366: E/AndroidRuntime(712): Caused by: java.lang.NullPointerException
**03-04 07:03:17.366: E/AndroidRuntime(712): at com.example.list_1.ClinicMessage.getViewByPosition(ClinicMessage.java:122)
03-04 07:03:17.366: E/AndroidRuntime(712): at com.example.list_1.ClinicMessage.AddBadgeView(ClinicMessage.java:104)**
03-04 07:03:17.366: E/AndroidRuntime(712): at com.example.list_1.ClinicMessage.onCreate(ClinicMessage.java:49)
03-04 07:03:17.366: E/AndroidRuntime(712): at android.app.Activity.performCreate(Activity.java:4465)
03-04 07:03:17.366: E/AndroidRuntime(712): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-04 07:03:17.366: E/AndroidRuntime(712): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
03-04 07:03:17.366: E/AndroidRuntime(712): ... 11 more
03-04 07:03:19.176: I/Process(712): Sending signal. PID: 712 SIG: 9
--------------------我的代码---------------------- ----
public class ClinicMessage extends Activity {
List<HashMap<String,Object>> mList= new ArrayList();
HashMap<String,Object> map=new HashMap();
private ListView mListView;
private ImageButton mIBBack;
private ImageButton mIBWrite;
private ImageButton imageButtonWithBadge;
MyBaseAdapter mBaseAdapter;
private ActionBar actionbar;
private BadgeView badge;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list1_clinic_message);
actionbar=getActionBar();
actionbar.hide();
initComponent();
initList();
**AddBadgeView();**
dealWithTitleBarEvents();
dealWithListEvents();
}
public void initComponent()
{
mListView=(ListView)findViewById(R.id.lvClinicMessage);
mIBBack=(ImageButton)findViewById(R.id.ibBack);
mIBWrite=(ImageButton)findViewById(R.id.ibWrite);
}
public void initList()
{
for(int i=0;i<8;i++)
{
map.put("ItemLeftPic",getResources().getDrawable(R.drawable.ib_cmsg_left));
map.put("ItemNext", getResources().getDrawable(R.drawable.im_arrow));
map.put("ItemTitle", "上海怡康家园");
map.put("ItemText", "医生你好,本周有空吗");
map.put("ItemTime", "14/12/14 17:30");
mList.add(map);
}
Toast.makeText(ClinicMessage.this, mList.size()+"项",Toast.LENGTH_LONG).show();
}
public void AddBadgeView()
{
**View view=getViewByPosition(0, mListView);**
ImageButton imageButtonWithBadge=(ImageButton) view.findViewById(R.id.ibClinicMessageLeft);
badge=new BadgeView(this, imageButtonWithBadge);
badge.setText(5);
badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
badge.show();
}
public View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
感谢帮助我T ^ T
-------------------------添加信息-------------
也许我没有把整个代码放进去,但是我通过setAdapter初始化了listView(你可以在dealWithListEvents()函数中看到它,并在oncreate()中调用。)。这里是dealWithListEvents():
public void dealWithListEvents()
{
mBaseAdapter=new MyBaseAdapter(mList, this);
mListView.setAdapter(mBaseAdapter);
mListView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View arg1, int position,long id)
{
Toast.makeText(ClinicMessage.this, "你点击了第"+position+"项",Toast.LENGTH_LONG).show();
}
});
}
----------------- MyBaseAdapter ---------------------
package com.example.list_1;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class MyBaseAdapter extends BaseAdapter {
private LayoutInflater mInflater; //得到一个LayoutInfalter对象用来导入布局
private List<HashMap<String,Object>>mList;
class ViewHolder
{
private ImageButton ibLeftPic;
private TextView tvTitle;
private TextView tvText;
private TextView tvTime;
private ImageView ivNext;
}
public MyBaseAdapter(List<HashMap<String,Object>> list,Context context) {
this.mInflater=LayoutInflater.from(context);
this.mList=list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewholder;
//convertView就是由于滑动被隐藏回收的ItemView
//viewHolder就是一个来接收它的缓存
if(convertView==null)
{
convertView=mInflater.inflate(R.layout.list1_listitem_clinicmessage, null);
viewholder=new ViewHolder();
viewholder.ibLeftPic=(ImageButton)convertView.findViewById(R.id.ibClinicMessageLeft);
viewholder.ivNext=(ImageView)convertView.findViewById(R.id.ivItemArrow);
viewholder.tvTitle=(TextView)convertView.findViewById(R.id.tvClinicMessagetTitle);
viewholder.tvText=(TextView)convertView.findViewById(R.id.tvClinicMessageText);
viewholder.tvTime=(TextView)convertView.findViewById(R.id.tvClinicMessageTime);
convertView.setTag(viewholder);
}
else
{
viewholder=(ViewHolder)convertView.getTag();
}
viewholder.ibLeftPic.setBackgroundDrawable((Drawable) mList.get(position).get("ItemLeftPic"));
viewholder.ibLeftPic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
viewholder.ivNext.setImageDrawable((Drawable)
mList.get(position).get("ItemNext"));
viewholder.tvTitle.setText(mList.get(position).get("ItemTitle").toString());
viewholder.tvText.setText(mList.get(position).get("ItemText").toString());
` `viewholder.tvTime.setText(mList.get(position).get("ItemTime").toString());`
return convertView;
}
public void refresh(List<HashMap<String,Object>> list)
{
list=mList;
notifyDataSetChanged();
}
}
答案 0 :(得分:1)
*假设您对listview
的初始化是正确的。
然后你的代码问题很可能就是
listView.getAdapter().getView(pos, null, listView);
您尚未初始化适配器,也没有为此listview
mBaseAdapter = new MyBaseAdapter(.....);
mListView.setAdapter(mBaseAdapter);
修改强>
发布新代码后。您必须切换调用初始化adapter
的方法以及调用它的方法的顺序。使用以下顺序
dealWithListEvents();
AddBadgeView();
答案 1 :(得分:0)
您在不使用适配器的情况下手动将项目添加到列表视图中。哪个是完全错误的。
您需要为列表视图设置自定义适配器,然后您的getViewByPosition(int pos, ListView listView)
方法才有效。
initComponent();
initList();
dealWithTitleBarEvents();
dealWithListEvents();
**AddBadgeView();**
试试这个..希望它有效