I have tried many methods but still not working. almost used all methods from stackoverflow's answers. but not working.
MainActivity.java
mProductList = new ArrayList<>();
//add sample data to list
mProductList.add(new Product(1, "iPhone4", 200, "Apple1"));
mProductList.add(new Product(2, "iPhone5", 2100, "Apple2"));
mProductList.add(new Product(3, "iPhone6", 2020, "Apple3"));
mProductList.add(new Product(4, "iPhone7", 2400, "Apple4"));
mProductList.add(new Product(5, "iPhone8", 2050, "Apple5"));
mProductList.add(new Product(6, "iPhone9", 7200, "Apple6"));
//init adapter
adapter = new ProductListAdapter(getApplicationContext(), mProductList);
lvProduct.setAdapter(adapter);
lvProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Clicked product id =" + view.getTag(), Toast.LENGTH_SHORT).show();
Product item = (Product)lvProduct.getAdapter().getItem(position);
Log.d("abc",item.getName());
Intent launchActivity1= new Intent(MainActivity.this,Show.class);
launchActivity1.putExtra("item", item.getName());
startActivity(launchActivity1);
}
});
and Show.java
public class Show extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show);
}
Intent launchActivity1 = getIntent();
String item = launchActivity1.getStringExtra("item"); // this is line 17
}
when i click on one item it shows item name in Log with "getName()" function. but at the same time app crashes and show error in Show.java on line 17
please help me to solve this code.
this error is showing in Log
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String)' on a null object reference
at com.example.sohaib.mydesign.Show.<init>(Show.java:17)