如何将字符串和图像数据从一个Activity传递到Android中的另一个Activity?

时间:2015-12-16 14:46:00

标签: java android android-activity

您好我是Android的新手,在我的项目中,我在MainActivity中创建了一个列表视图,当我点击列表视图行时,我想在我的DetailActivity上显示详细信息(这里我想显示userimage, name,desc)。

为此我写了下面的代码,但是当我点击ListView行时,异常就像(NullPointerException)一样。我在这做错了什么?

MainActivity: -

String[] nameArray;
String[] descArray;
int[] images;

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

    Intent detail=new Intent(MainActivity.this,DetailActivity.class);
    detail.putExtra("titles",nameArray[position]);
    detail.putExtra("description", descArray[position]);
    detail.putExtra("images",images[position]);
    startActivity(detail);
}

DetailActivity: -

ImageView Image;
TextView Name;
TextView Desc;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Image = (ImageView)findViewById(R.id.imageView2);
    Name = (TextView)findViewById(R.id.textView);
    Desc = (TextView)findViewById(R.id.textView3);

    Bundle bundle = getIntent().getExtras();
    String name = bundle.getString("titles");
    String desc = bundle.getString("description");
    String image = bundle.getString("images");

    Image.setImageResource(image);
    Name.setText(name);
    Desc.setText(desc);
}

1 个答案:

答案 0 :(得分:4)

您没有在detailActivity

中的onCreate方法中附加xml 像这样

的setContentView(R.layout.activity_dash_board_main);