0

I am working on Custom ListView for an assignment given to me. I have worked out a custom list view by using Base Adapter. I have a Button in the view by clicking on which I want new activity to be started. But I am getting error on run time. Please note that BUTTON is not the part of the listview, I mean its not repeating, Its placed only once at the top of the layout with Listview included.

public class mainActivity2 extends Activity {

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

    ArrayList<ItemInfo> items = GetSearchResults();
    final ListView lv1 = (ListView) findViewById(R.id.mylistview);
   if(lv1==null){

        Log.d("error","ListView is null");
    }

    lv1.setAdapter(new itemBaseAdapter(this, items));
    final Button btnShowCart = (Button) findViewById(R.id.shCart);
    btnShowCart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
              Intent myIntent = new Intent(v.getContext(), Cart.class);
               startActivity(myIntent);         

        }
    });   


}

private ArrayList<ItemInfo> GetSearchResults() {

    ArrayList<ItemInfo> items=new ArrayList<ItemInfo>();
    items.add(new ItemInfo("pizza",R.drawable.icon,15));
    items.add(new ItemInfo("chicken",R.drawable.icon,10));
    items.add(new ItemInfo("slice",R.drawable.icon,12));
    items.add(new ItemInfo("hoha",R.drawable.icon,20));

    return items;
}

}

StartActivity(myIntent) is the line I am getting error at run time. Here is the code to it, I will be grateful if somebody shed light on the problem.

Rookie
  • 597
  • 3
  • 8
  • 18

5 Answers5

4

Did you add Cart.class to manifest file? What is error log?

Community
  • 1
  • 1
Dmytro Danylyk
  • 19,684
  • 11
  • 62
  • 68
1

Rather than new Intent(v.getContext, Cart.class), try:

Intent myIntent = new Intent(this, Cart.class);
Sparky
  • 8,437
  • 1
  • 29
  • 41
1

1) Maybe the problem lays in the v.getContext() method. Try this:

Intent intent = new Intent(mainActivity2.this, Cart.class);
startActivity(intent);

2) Maybe you did not register 'Cart' activity in the Manifest file.

Taras Feschuk
  • 689
  • 1
  • 6
  • 8
0

You can just replace v.getContext() with mainActivity2.this.

Sam
  • 86,580
  • 20
  • 181
  • 179
Saurabh Verma
  • 6,328
  • 12
  • 52
  • 84
0

First, check whether your second activity has been added to your Manifest.xml file. And, after try with this code --

Intent i = new Intent(mainActivity2.this, Cart.class);
startActivity(i);

Maybe, it will helpful to you.

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173