动态创建自定义视图:Resources $ NotFoundException

时间:2018-10-07 19:11:53

标签: java android android-layout

我想动态创建自定义视图(即没有任何XML文件)。这是我的课:

public class MyCustomView extends LinearLayout {
    Context context;

    public MyCustomView(Context context) {
        super(context);
        this.context = context;
    }

    public void addInfo(String path, String text){
        ImageView photo = new ImageView(context);
        photo.setImageBitmap(getBitmapFromAsset(context,path));
        this.addView(photo);

        TextView name = new TextView(context);
        name.setText(text);
        this.addView(name);
    }
}

在我的Main中:

LinearLayout container = findViewById(R.id.linearLayout);

MyCustomView cv = new MyCustomView(getApplicationContext());
cv.addInfo("myPath","myText");
container.addView(cv);

但是我得到了addView以下异常:android.content.res.Resources$NotFoundException: String resource ID #0x0

我想某些内容没有正确初始化...

-编辑- 我犯了一个错误,使我尝试插入和插入的Text的内容不匹配...并且某种程度上它没有在构建时崩溃,而只是在运行时崩溃

0 个答案:

没有答案
相关问题