【问题标题】:Not able to open the image in new activity无法在新活动中打开图像
【发布时间】:2012-05-03 13:24:43
【问题描述】:

需要帮助。这可能很奇怪。

第一个活动具有列表视图(如lazyadapter),一旦我单击列表,它就会打开一个带有描述、标题和图像的新活动。但我无法显示图像。不知道如何发送它......

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


String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
String description = ((TextView) view.findViewById(R.id.artist)).getText().toString();
String pdate = ((TextView)view.findViewById(R.id.pdate)).getText().toString();
//ImageView thumb = ((ImageView) view.findViewById(R.id.list_image));

// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
    in.putExtra(KEY_PLAYER, title);
    in.putExtra(KEY_THUMB_URL, R.id.list_image);
    in.putExtra(KEY_TRANSACTION, description);
    in.putExtra(KEY_PUBDATE, pdate);
    startActivity(in);  }

我的第二个活动

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent in = getIntent();

    String title = in.getStringExtra(KEY_PLAYER);
    String description = in.getStringExtra(KEY_TEXT);
    String pdate = in.getStringExtra(KEY_PUBDATE);
    String thumb_image = in.getStringExtra(KEY_THUMB_URL);

    TextView lblName = (TextView) findViewById(R.id.name_label);
    TextView lblDesc = (TextView) findViewById(R.id.description_label);
    TextView lblPdate = (TextView) findViewById(R.id.pubdate_label);
    ImageView thumb = (ImageView) findViewById(R.id.thumb_label);

    lblName.setText(title);
    thumb.setImageURI(Uri.parse(thumb_image));
    lblDesc.setText(description);
    lblPdate.setText(pdate);  

【问题讨论】:

  • 你能直接从 Second Activity 的资源中设置图片吗?不是来自 getStringExtra..

标签: android imageview android-imageview


【解决方案1】:
int thumb_image = in.getIntExtra(KEY_THUMB_URL);
thumb.setImageResource(thumb_image);

您正在将资源 ID(整数)传递给您的意图。当您收到该意图时,您使用getIntExtra 提取它,这将为您提供R.id.list_image 的值。然后你可以在你的 ImageView 上调用setImageResource 来设置图片。

当你传入一个整数时调用getStringExtra将导致一个空字符串。

【讨论】:

  • 太棒了!!多谢你们 !!这显示没有错误!我是 android 新手,所以这对我来说是新事物……我在新活动中看不到图像……我在 logcat W/ImageView(5381) 中看到以下内容:android.content.res.Resources$ NotFoundException:资源不是可绘制对象(颜色或路径):TypedValue{t=0x12/d=0x0 a=2 r=0x7f050034}
  • R.id.list_image 可能是视图(imageview、textview 等)的资源 ID。要传递实际图像的 ID,它类似于 R.drawable.image;显然,当您将文件放在可绘制文件夹中时,“图像”将是您命名文件的任何名称
  • 感谢 Dymmeh 和 Perroloco 的所有帮助!这样可行 !!宾果游戏:)
猜你喜欢
  • 2018-07-04
  • 1970-01-01
  • 2018-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多