在android中列出查看问题

时间:2011-07-28 14:11:27

标签: android android-listview

我有一个名为'edit'的按钮,在按钮下面我有一个列表视图。默认情况下,在列表中隐藏减号图像按钮并删除图像按钮。如果我单击编辑按钮减去图像按钮以显示所有列表项目,如果我触摸项目的减号图像按钮,则显示相应列表项目的删除按钮。如果我触摸删除按钮,则删除相应的行。如果单击编辑按钮,我会执行以下操作,我会得到空指针异常。

public class Favorite extends ListActivity implements OnClickListener{


ImageButton delete,del1,del2,editbutton;
private Context context;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.favorite_list_view);

    editbutton = (ImageButton)findViewById(R.id.editbutton);

    del1 = (ImageButton)findViewById(R.id.delete1);


    editbutton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View view) 
        {
           del1.setVisibility(View.VISIBLE);  // line no 69 . i get null pointer exception here. 
        }

    });        
    String favorite_list[];
    int count=0;

    //      database coding ....
    ..........              

   setListAdapter(new ArrayAdapter<String>(this,R.layout.favorite_row_view, R.id.text1, favorite_list));



} 
protected void onListItemClick(ListView l, View convertView, int position, long id) {
    super.onListItemClick(l, convertView, position, id);

    View view;

    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater)
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.favorite_row_view, null);

        del1 = (ImageButton)view.findViewById(R.id.delete1);
        delete = (ImageButton)view.findViewById(R.id.delete2);

        del1.setOnClickListener(this);
        delete.setOnClickListener(this);

    }

    else {
        view = convertView;
    }

    del1 = (ImageButton)view.findViewById(R.id.delete1);
    delete = (ImageButton)view.findViewById(R.id.delete2);

    del1.setTag(position);
    delete.setTag(position);



    Object o = this.getListAdapter().getItem(position);
    String pen = o.toString();
    Toast.makeText(this, ""+position + "-" +"fid - "+""+favoritelist.get(position)+ pen, Toast.LENGTH_SHORT).show();
    Bundle bundle = new Bundle();
    ..........
    Intent myIntent = new Intent(convertView.getContext(), favorite_swipe.class);
    myIntent.putExtras(bundle);
    startActivityForResult(myIntent, 0);

}
@Override
public void onClick(View v) {
    Integer position = (Integer) v.getTag();
    switch(v.getId()){
    case R.id.delete1:
        Toast.makeText(this,position+"- delete1", Toast.LENGTH_SHORT).show();
        del1.setVisibility(View.VISIBLE);
        delete.setVisibility(View.VISIBLE);
    break;  
    case R.id.delete2:
        Toast.makeText(this,position+"-delete2", Toast.LENGTH_SHORT).show();
        del1.setVisibility(View.VISIBLE);
        delete.setVisibility(View.VISIBLE);
    break;          
    }

}

}

log cat:

  07-28 19:38:21.019: ERROR/AndroidRuntime(9672): FATAL EXCEPTION: main
  07-28 19:38:21.019: ERROR/AndroidRuntime(9672): java.lang.NullPointerException
  07-28 19:38:21.019: ERROR/AndroidRuntime(9672):     at com.sql.test.Favorite$3.onClick(Favorite.java:69)
  07-28 19:38:21.019: ERROR/AndroidRuntime(9672):     at android.view.View.performClick(View.java:2408)
  07-28 19:38:21.019: ERROR/AndroidRuntime(9672):     at android.view.View$PerformClick.run(View.java:8816)

1 个答案:

答案 0 :(得分:0)

这是最后一个......

ListView list = favorite_list_view.findViewById(R.id.lisViewId);
Button b= list.findViewById(R.id.buttonId);

等等。如果你给listView和Button

提供适当的ID,这必须有效