【问题标题】:Unable to pass object to another Android activity(with parcelable)无法将对象传递给另一个 Android 活动(使用 parcelable)
【发布时间】:2017-07-22 05:23:17
【问题描述】:

//ListSalesItemActivity(这是一个带有listview的活动,当我点击列出的一个项目时,我将转到ViewDetailActivity并显示数据)

我能够获取数据,但无法传递给另一个活动,应用程序将停止

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

    myContext = this;


    salesLV = (ListView) findViewById(R.id.salesLV);


    salesLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Bundle bundle = new Bundle();


            Intent myIntent = new Intent(ListSaleItemActivity.this, SaleDetailActivity.class);

            SaleObject SO = mc.SalesObjectList(getApplicationContext()).get(position);
            Log.d("myTag", SO.getStrName());//able to get name
            bundle.putString("Name", SO.getStrName());
            myIntent.putExtras(bundle);
            startActivity(myIntent);


        }
    });

}

//查看详细活动

Intent i = getIntent();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sale_detail);
    {

        Bundle bd = i.getExtras();
        String name = bd.getString("Name");
        Log.d("myTag", name);//does not print anything in logcat and application stopped.

        //I tried this and also does not work
        /*if(bd != null)
        {
            String getName = (String) bd.get("Name");
            Log.d("myTag", getName);
        }else{
            Log.d("myTag", "1");
        }*/

    }
}

【问题讨论】:

  • 如果有贴过log(错误报告),在编码前很容易发现错误

标签: android listview android-intent


【解决方案1】:

把 i = getIntent();在 onCreate() 中。

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

    i = getIntent();
    Bundle bd = i.getExtras();
    String name = bd.getString("Name");
    Log.d("myTag", name);

}

}

【讨论】:

    猜你喜欢
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多