Android错误在listview中的单击侦听器上实现图像

时间:2014-01-28 12:47:29

标签: android

您好,我是Android编程新手

我已经成功创建了一个listview应用程序 在每个列表视图中,左侧有一个图像,屏幕右侧有文本

我想要实现的目标: 当用户点击图像时,它会将他链接到另一个页面 错误:即使我使用的是接口

,我也会遇到错误

我的代码如下 请帮帮我:

 public interface OnImageClickListener {
   public void onClick(View view); // Object data [Optional]
 }

CustomListViewAndroidExample.java

   public class CustomListViewAndroidExample extends Activity implements  

   OnImageClickListener {
      ....More code
      public void onClick(View view) {
    // TODO Auto-generated method stub
    Intent intent = new  

            Intent(getApplicationContext(),SingleLockerActivity.class);
    startActivity(intent);
}
   }

CustomAdapter.java

  public class CustomAdapter extends BaseAdapter   implements OnClickListener {

     public View getView(int position, View convertView, ViewGroup parent) {

    View vi=convertView;
    ViewHolder holder;

    if(convertView==null){ 


        vi = inflater.inflate(R.layout.tabitem, null); 

        /******** View Holder Object to contain tabitem.xml file elements ************/
        holder=new ViewHolder();
        holder.text=(TextView)vi.findViewById(R.id.text);
        holder.text1=(TextView)vi.findViewById(R.id.text1);
        holder.image=(ImageView)vi.findViewById(R.id.image);

       /************  Set holder with LayoutInflater ************/
        vi.setTag(holder);
    }
    else  
        holder=(ViewHolder)vi.getTag();

    if(data.size()<=0)
    {
        holder.text.setText("No Data");

    }
    else
    {
        /***** Get each Model object from Arraylist ********/
        tempValues=null;
        tempValues = (ListModel) data.get(position);

        /************  Set Model values in Holder elements ***********/
         holder.text.setText(tempValues.getCompanyName());
         holder.text1.setText(tempValues.getUrl());


              holder.image.setImageResource(res.getIdentifier
              ("com.example.customlistview24:drawable
              /"+tempValues.getImage(),null,null));


         vi.setOnClickListener(new OnItemClickListener(position));
         }
        //Gabriel Code 28/01/2014
        ImageView imageView = (ImageView) vi.findViewById(R.id.image);
        imageView.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {


            Log.v("23455", "12355");
            try {

                mListener = new CustomListViewAndroidExample();
            } catch (ClassCastException e) {
                throw new 
                            ClassCastException(activity.toString() + "  
                            must implement OnFileSelectedListener");
            }

            if (mListener != null) {
                Log.v("234", "123");
                //mListener.onClick(v);


            }

        }


    });


             return vi;
           }

            }

错误在于CustomAdapter.java 在这一行:

   mListener = new CustomListViewAndroidExample();
    logcat:java.lang.nullpointerException

请帮帮我,我读了许多其他stackoverflow文章,但我仍然卡住了

CustomListViewAndroidExample
public class CustomListViewAndroidExample extends Activity implements OnImageClickListener {

ListView list;
CustomAdapter adapter;
public  CustomListViewAndroidExample CustomListView = null;
public  ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom_list_view_android_example);

    CustomListView = this;

    setListData();

    Resources res =getResources(); 
            list=(ListView)findViewById(R.id.list);

             /**************** Create Custom Adapter *********/
             adapter=new CustomAdapter(CustomListView, CustomListViewValuesArr,res);
               list.setAdapter(adapter);

          }

            /****** Function to set data in ArrayList *************/
                  public void setListData()
                 {

          for (int i = 0; i < 11; i++) {

        final ListModel sched = new ListModel();

          /******* Firstly take data in model object ******/
           sched.setCompanyName("Company "+i);
           sched.setImage("image"+i);
           sched.setUrl("http:\\\\www."+i+".com");

        /******** Take Model Object in ArrayList **********/
        CustomListViewValuesArr.add(sched);
        }

                }

              public void onItemClick(int mPosition)
              {
                 ListModel tempValues = (ListModel) 

                CustomListViewValuesArr.get(mPosition);

                  Toast.makeText(CustomListView, 
            ""+tempValues.getCompanyName()+" \nImage:"+tempValues.getImage()+" 

                    \nUrl:"+tempValues.getUrl(), 
            Toast.LENGTH_LONG)
                      .show();
                    }

    @Override
     public void onClick(View view) {
    // TODO Auto-generated method stub
    Intent intent = new                     

             Intent(getApplicationContext(),SingleLockerActivity.class);
    startActivity(intent);
       }


          }

1 个答案:

答案 0 :(得分:0)

在mListener = new CustomListViewAndroidExample();

为什么你的mListener不是null?(请粘贴相关代码)和CustomListViewAndroidExample 活动