如何从Listview获取另一项活动的项目详细信息

时间:2016-06-16 13:40:17

标签: android

我尝试了很多方法,但仍然无法正常工作。 几乎使用了stackoverflow的所有方法的答案。但没有工作。

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

和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
}

当我点击一个项目时,它在日志中显示项目名称&#34; getName()&#34;功能。 但同时app在第17行的Show.java中崩溃并显示错误

请帮我解决这个问题。

此错误显示在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)

4 个答案:

答案 0 :(得分:0)

您的代码在onCreate方法之外!

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

把它放在onCreate上。 onCreate也没有覆盖注释。

答案 1 :(得分:0)

  

将此粘贴​​复制到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
}

}

答案 2 :(得分:0)

将它添加到oncreate block ...

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


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

或者当您传递对象而不是字符串时会发生这种情况。所以使用

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

感谢..

并且onclick应该是..

Product item = (Product)parent.getAdapter().getItem(position);

答案 3 :(得分:0)

我已经解决了这个问题。谢谢大家的快速回复...解决方案是......

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