OnItemClickListener对某些项目的NullPointerException,但不是所有项目

时间:2015-08-11 20:42:28

标签: java android listview onitemclicklistener

我编写了一个OnItemClickListener,因为我一直用来编码它:

@Override
protected AdapterView.OnItemClickListener setDrawerClickListener(){
    AdapterView.OnItemClickListener customListener=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent lancia=null;
            HomeActivity.this.closeDrawer();
            try {
                switch (i) {
                    case 0:
                        log.d("CLICK","I0: "+i);
                        break;
                    case 1:
                        log.d("CLICK","I1: "+i);
                        if (makeLayout.getVisibility() == View.VISIBLE) {
                            hideMakeMenu();
                            showMenu();
                        }
                        HomeActivity.this.changeDrawerSelected(new boolean[]{false, true, false, false, false, false, false, false});
                        break;
                    case 2:
                        log.d("CLICK","I2: "+i);
                        break;
                    case 3:
                        log.d("CLICK","I3: "+i);
                        break;
                    case 4:
                        log.d("CLICK","I4: "+i);
                        if (makeLayout.getVisibility() == View.GONE) {
                            hideMenu();
                            showMakeMenu();
                        }
                        HomeActivity.this.changeDrawerSelected(new boolean[]{false, false, false, false, true, false, false, false});
                        break;
                    case 5:
                        log.d("CLICK","I5: "+i);
                        //lancia = new Intent("com.recquest.LIST");
                        //lancia.putExtra("WHICH", "AROUND");
                        showMakeMenu();
                        break;
                    case 6:
                        log.d("CLICK","I6: "+i);
                        lancia = new Intent(HomeActivity.this,HomeActivity.class);
                        lancia.putExtra("WHICH", "MINE");
                        break;
                    case 7:
                        log.d("CLICK","I7: "+i);
                        break;
                }
                if (lancia != null)
                    startActivity(lancia);
            }catch(Exception e){}
        }
    };
    return customListener;
}

它在方法内部,因为我的活动扩展了我自己创建的另一个BaseActivity。单击一个项目时,日志会正确显示按下了哪一个。问题是案例1和4完美地工作,但其他案例在自定义适配器的getView方法中抛出NullPointerException(listView的渲染是完美的)。 以下是我初始化适配器的方法,可以看出,所有项目都以相同的方式初始化,然后在屏幕上很好地呈现:

private DrawerItem[] initializeDrawerArray(boolean[] selected){
    DrawerItem[] pz=new DrawerItem[8];
    if(selected[0]){
        pz[0]=new DrawerItem(null,DrawerItemType.PROFILE_ITEM,0,true);
    }else{
        pz[0]=new DrawerItem(null,DrawerItemType.PROFILE_ITEM,0,false);
    }
    if(selected[1]){
        pz[1]=new DrawerItem(getResources().getString(R.string.drawer_home),DrawerItemType.REGULAR_ITEM,R.drawable.home_red,true);
    }else{
        pz[1]=new DrawerItem(getResources().getString(R.string.drawer_home),DrawerItemType.REGULAR_ITEM,R.drawable.home,false);
    }
    pz[2]=new DrawerItem(null,DrawerItemType.DIVIDER,0,false);
    if(selected[3]){
        pz[3]=new DrawerItem(getResources().getString(R.string.drawer_most),DrawerItemType.REGULAR_ITEM,R.drawable.most_red,true);
    }else{
        pz[3]=new DrawerItem(getResources().getString(R.string.drawer_most),DrawerItemType.REGULAR_ITEM,R.drawable.most,false);
    }
    if(selected[4]){
        pz[4]=new DrawerItem(getResources().getString(R.string.drawer_new),DrawerItemType.REGULAR_ITEM,R.drawable.newreq_red,true);
    }else{
        pz[4]=new DrawerItem(getResources().getString(R.string.drawer_new),DrawerItemType.REGULAR_ITEM,R.drawable.newreq,false);
    }
    if(selected[5]){
        pz[5]=new DrawerItem(getResources().getString(R.string.drawer_around),DrawerItemType.REGULAR_ITEM,R.drawable.aroundme_red,true);
    }else{
        pz[5]=new DrawerItem(getResources().getString(R.string.drawer_around),DrawerItemType.REGULAR_ITEM,R.drawable.aroundme,false);
    }
    if(selected[6]){
        pz[6]=new DrawerItem(getResources().getString(R.string.drawer_my),DrawerItemType.REGULAR_ITEM,R.drawable.myreq_red,true);
    }else{
        pz[6]=new DrawerItem(getResources().getString(R.string.drawer_my),DrawerItemType.REGULAR_ITEM,R.drawable.myreq,false);
    }
    if(selected[7]){
        pz[7]=new DrawerItem(getResources().getString(R.string.drawer_browse),DrawerItemType.REGULAR_ITEM,R.drawable.live_red,true);
    }else{
        pz[7]=new DrawerItem(getResources().getString(R.string.drawer_browse),DrawerItemType.REGULAR_ITEM,R.drawable.live,false);
    }
    return pz;
}

这个初始化方法在BaseActivity中,我的活动扩展了。

之前有人遇到此错误吗?这很奇怪,因为即使执行开关盒内没有任何东西我也得到了错误

编辑: 这是StackTrace

08-11 22:49:15.429  13775-13775/com.recquest.recquestapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.recquest.recquestapp.DrawerAdapter.getView(DrawerAdapter.java:63)
        at android.widget.AbsListView.obtainView(AbsListView.java:1588)
        at android.widget.ListView.makeAndAddView(ListView.java:1772)
        at android.widget.ListView.fillSpecific(ListView.java:1315)
        at android.widget.ListView.layoutChildren(ListView.java:1601)
        at android.widget.AbsListView.onLayout(AbsListView.java:1439)
        at android.view.View.layout(View.java:7175)
        at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:931)
        at android.view.View.layout(View.java:7175)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
        at android.view.View.layout(View.java:7175)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
        at android.view.View.layout(View.java:7175)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
        at android.view.View.layout(View.java:7175)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
        at android.view.View.layout(View.java:7175)
        at android.view.ViewRoot.performTraversals(ViewRoot.java:1144)
        at android.view.ViewRoot.handleMessage(ViewRoot.java:1863)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:130)
        at android.app.ActivityThread.main(ActivityThread.java:3687)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
        at dalvik.system.NativeStart.main(Native Method)

EDIT2:

public class DrawerAdapter extends BaseAdapter {
DrawerItem[] items;
Context mCtx;
int layout;
PicManager mPicManager;
MyLog log=new MyLog();
public DrawerAdapter(Context ctx,int layout,DrawerItem[] items){
    this.items=items;
    this.mCtx=ctx;
    this.layout=layout;
    mPicManager=new PicManager();
}

@Override
public int getCount(){
    return items.length;
}
@Override
public Object getItem(int i){
    return items[i];
}
@Override
public long getItemId(int arg0){
    return arg0;
}
@Override
public View getView(int arg0,View arg1,ViewGroup arg2){
    View row=arg1;
    DrawerItemHolder holder=null;
    if(row==null){
        LayoutInflater inflater=((Activity)mCtx).getLayoutInflater();
        row=inflater.inflate(layout,arg2,false);
        holder=new DrawerItemHolder();
        holder.back=(RelativeLayout)row.findViewById(R.id.background);
        holder.layout=(RelativeLayout)row.findViewById(R.id.layout);
        holder.profilePic=(SvgImageView)row.findViewById(R.id.customShapeImageView);
        holder.divisorio=(ImageView)row.findViewById(R.id.imageView2);
        holder.icon=(ImageView)row.findViewById(R.id.imageView1);
        holder.text=(TextView)row.findViewById(R.id.textView1);
    }else{
        holder=(DrawerItemHolder)row.getTag();
    }
    switch(items[arg0].type) {
        case DrawerItemType.PROFILE_ITEM:
            holder.icon.setVisibility(View.GONE);
            holder.divisorio.setVisibility(View.GONE);
            if (items[arg0].selected) {
                holder.text.setTextColor(this.mCtx.getResources().getColor(R.color.dark_red));
                holder.back.setBackgroundColor(this.mCtx.getResources().getColor(R.color.selected_menu_back));
                holder.layout.setBackgroundColor(this.mCtx.getResources().getColor(R.color.selected_menu_back));
            }
            if (mPicManager.getMyPic() != null) {
                holder.profilePic.setImageBitmap(mPicManager.getMyPic());
            } else {
                mPicManager.downloadMyPic();
            }
            LocalProfile local = LocalProfile.getLocalProfile(mCtx);
            if (local != null)
                holder.text.setText(local.nome.toUpperCase() + " " + local.cognome.toUpperCase());
            break;
        case DrawerItemType.REGULAR_ITEM:
            holder.profilePic.setVisibility(View.GONE);
            holder.divisorio.setVisibility(View.GONE);
            holder.icon.setImageDrawable(this.mCtx.getResources().getDrawable(items[arg0].resID));
            holder.text.setText(items[arg0].name);
            if (items[arg0].selected) {
                holder.text.setTextColor(this.mCtx.getResources().getColor(R.color.dark_red));
                holder.back.setBackgroundColor(this.mCtx.getResources().getColor(R.color.selected_menu_back));
                holder.layout.setBackgroundColor(this.mCtx.getResources().getColor(R.color.selected_menu_back));
            }
            break;
        case DrawerItemType.DIVIDER:
            holder.profilePic.setVisibility(View.GONE);
            holder.icon.setVisibility(View.GONE);
            holder.text.setVisibility(View.GONE);
            holder.divisorio.setVisibility(View.VISIBLE);
            break;
    }
    return row;
}

class DrawerItemHolder{
TextView text;
SvgImageView profilePic;
ImageView icon,divisorio;
RelativeLayout back,layout;

1 个答案:

答案 0 :(得分:0)

在DrawerAdapter类的第63行检查代码,这是应用程序崩溃的地方。您正在使用一些尚未初始化的对象并使用它进行调用。

在初始化对象的条件处放置断点。逐步完成代码。这里没有足够的信息来说明对象未初始化的原因。把行号放在draweradapter类中也没什么坏处。

What is a NullPointerException, and how do I fix it?