2

There are several points i wanna to provide here:

  1. each item of listview consists an imageButton,2 TextView and 1 imageView

  2. I want to get the imageButton of the itemView in the activity via position rather than the click event because i wanna add a badgeView to the imageButton

  3. The error information is as listed:

--------------------error information----------------

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

--------------------My Code--------------------------

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);
    }
}

thanks for helping me T^T

-------------------------added info-------------

perhaps i didn't put the whole code in ,but i did initialize the listView by setAdapter (you can see it in the dealWithListEvents()function and the call in oncreate().).here is the 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();

            }   
        });
    }

-----------------the 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();
}

}

lizlalala
  • 311
  • 4
  • 15
  • Where are you setting adapter to your listview – Preethi Rao Mar 04 '15 at 07:31
  • Yea probably `listView.getAdapter()` causing the nullpointer since you haven't set an adapter on it. From the looks of `initList()` it seems you need to create a custom adapter for you list. – cYrixmorten Mar 04 '15 at 07:37
  • Hint `ClinicMessage.java:122` – Maveňツ Mar 04 '15 at 07:39
  • sorry,perhaps i didn't put the whole code for it. but i did inialize the listview by setAdapter in "dealWithListEvents()"(you can see it in the oncreate function) – lizlalala Mar 04 '15 at 08:17
  • you are calling AddBadgeView(); before calling dealWithListEvents(); ie you calling getview on listview before setting adapter – Preethi Rao Mar 04 '15 at 08:29
  • 1
    @PreethiRao, thank u, you're right,it worked!, but there is still some problem that i don't know why the badgeview doesn't show – lizlalala Mar 04 '15 at 08:51
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Albireo Mar 07 '16 at 08:56

2 Answers2

1

*Assuming that your initialization for the listview is correct.

Then the problem with your code most probably will be

listView.getAdapter().getView(pos, null, listView);

You have not initialized the adapter yet and also have not set the adapter for this listview

mBaseAdapter = new MyBaseAdapter(.....);
mListView.setAdapter(mBaseAdapter);

EDIT

After posting your new code. You have to switch the order of calling the method where you initialize the adapter and the method you call it. Use the following order

dealWithListEvents();
AddBadgeView();    
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
  • sorry ,but i did setup myBaseAdapter ,perhaps i didn't show it in my question. please see the bottom of the question ,i have added some more info to it. – lizlalala Mar 04 '15 at 08:28
  • @lizlalala you are trying to use the `adapter` in `AddBadgeView()` method before calling the method that initialize the `adapter` in your method `dealWithListEvents();` ... Just call it before use it – Sami Eltamawy Mar 04 '15 at 09:41
  • your order should change,dealWithListEvents() first~~ – lizlalala Mar 04 '15 at 15:34
  • Yes, @lizlalala .. but I have explained it and I guess u know what I'm talking about..Always make sure to initialize the anything probably before using it – Sami Eltamawy Mar 05 '15 at 05:48
  • ,i will remember that,thk u:), but the badge view doesn't show,could you give some advice? – lizlalala Mar 05 '15 at 06:53
  • Actually @lizlalala I have not worked with the custom BadgeView that you are dealing with. I prefer to implement it by myself using `FrameLayout` or `RelativeLayout` and control its visibility and its value according to my needs – Sami Eltamawy Mar 05 '15 at 07:15
0

You are manually adding items to your listview without using an adapter. Which is completely wrong.

You need to setup a custom Adapter for your listview,Then your getViewByPosition(int pos, ListView listView) method will work.

initComponent();
initList();
dealWithTitleBarEvents();
dealWithListEvents();
**AddBadgeView();**

try this.. Hope it works

Preethi Rao
  • 5,117
  • 1
  • 16
  • 29
  • sorry ,but i did setup myBaseAdapter ,perhaps i didn't show it in my question,please see the question again and see some more info i have added,thx :) – lizlalala Mar 04 '15 at 08:27