【问题标题】:Dynamic created ImageView can not be get by id Android?动态创建的 ImageView 无法通过 id Android 获取?
【发布时间】:2015-02-12 02:02:22
【问题描述】:

我在获取带有 id 的 imageview 控件时出错

错误是,

12-13 08:06:56.914: E/AndroidRuntime(1967): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://media/external/images/media/26 }} to activity {com.example.hakslogin/com.example.hakslogin.QuestionActivityDynamic}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.ImageView

获取它的代码是,

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                String imgid=String.valueOf(requestCode); 
                img = (ImageView) findViewById(requestCode);
                img.setImageURI(selectedImageUri);
}

这里我没有提到动态创建imageview的代码,但是我的imageview id是0。

在线出错,

img = (ImageView) findViewById(requestCode);

希望您的建议

【问题讨论】:

  • 你调用 startActivityForResult() 方法的地方,也贴出代码。

标签: java android dynamic imageview android-imageview


【解决方案1】:

.ClassCastException: android.widget.Button 无法转换为 android.widget.ImageView 您正在将名为 img 的 Button 投射到图像视图

尝试将 set id 重命名为另一个 int

【讨论】:

  • 刚发帖就遇到了这个问题,感谢您的回复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多