list view problem in android
i have a button called 'edit' and below the button i have a list view. By default in the list i hide the minus image button and delete image button. if i click edit button minus image button to be shown for all list items and if i touch a minus image button of a item the corresponding list item's delete button to be shown. and if i touch the delete button the corresponding row to be deleted. i did the following if click edit button i get null pointer exception.
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 = 开发者_如何学Gothis.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)
here is the final one..
ListView list = favorite_list_view.findViewById(R.id.lisViewId);
Button b= list.findViewById(R.id.buttonId);
and etc.. this has to work if you are giving proper IDs to listView and Button
精彩评论