0

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)
Sohaib Riaz
  • 21
  • 1
  • 7

4 Answers4

0

Your code is outside onCreate method!

Intent launchActivity1 = getIntent();
String item = launchActivity1.getStringExtra("item");

Put this inside onCreate. Also onCreate doesn't have override annotation.

jitinsharma
  • 1,436
  • 13
  • 22
  • and please tell me how to create onCreate override annotation – Sohaib Riaz Jun 16 '16 at 13:53
  • Put `@Override` on line above `onCreate` The code which you shared has Intent inside activity block but outside onCreate. Check again! – jitinsharma Jun 16 '16 at 13:57
  • The annotation doesn't do anything special other than tell the compiler that the method should be overriden. Simply having the same method signature is what actually overrides the method – OneCricketeer Jun 16 '16 at 13:59
0

Copy paste this instead in your Show.java

public class Show extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.show);
    Intent launchActivity1 = getIntent();
    String item = launchActivity1.getStringExtra("item"); // this is line 17
}

}

  • `item.getName()` does this method return a string? – jitinsharma Jun 16 '16 at 14:05
  • when i move my cursor on item in Product item = (Product)parent.getAdapter().getItem(position); it says varriable 'item' is never used. and in show.java it show varriable 'item' is never used at this line... String item = launchActivity1.getStringExtra("item"); – Sohaib Riaz Jun 17 '16 at 05:09
0

add it in oncreate block...

public class Show extends Activity {
 protected void onCreate(Bundle savedInstanceState) {
           ................
           ................


     Intent launchActivity1 = getIntent();
    String item = launchActivity1.getStringExtra("item"); // this is line 17
    }
}

Or it happens when you pass object instead of string. So use

launchActivity1.putExtra("item", ""+item.getName());

thanks..

and in onclick should be..

Product item = (Product)parent.getAdapter().getItem(position);
Kush
  • 1,080
  • 11
  • 15
  • when i move my cursor on item in Product item = (Product)parent.getAdapter().getItem(position); it says varriable 'item' is never used. – Sohaib Riaz Jun 17 '16 at 05:03
  • and same in both pages Mainactivity and Show.java in show.java it show varriable 'item' is never used at this line... String item = launchActivity1.getStringExtra("item"); – Sohaib Riaz Jun 17 '16 at 05:06
  • did you try this? "String item= getIntent().getExtras().getString("item");" – Kush Jun 17 '16 at 05:46
  • not working. why this happening to me. i am getting value in log but can't pass to another activity. :( – Sohaib Riaz Jun 17 '16 at 06:22
  • when i click i get value in Log 06-17 11:21:21.758 9567-9567/com.example.sohaib.mydesign D/abc: iPhone4 – Sohaib Riaz Jun 17 '16 at 06:23
0

I have Solved the issue on my own. thank you all of you for your quick response... solution is...

MainActivity.java

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);

                Intent launchActivity1= new Intent(MainActivity.this,Show.class);
                launchActivity1.putExtra("item", item.getName());
                startActivity(launchActivity1);


            }
        });

Show.java

public class Show extends Activity {

    String passedVar = null;
    private TextView passedView= null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);
       passedVar = getIntent().getStringExtra("item");
        passedView = (TextView) findViewById(R.id.pass);
        passedView.setText("You= " + passedVar);
    }
}
Sohaib Riaz
  • 21
  • 1
  • 7