[英]How to create ImageView from another class
我想从我的班级创建 ImageVIew,但我有一些例外,不知道为什么。 请帮我。 这是我的课
公共类卡片{
Bitmap bitmap;
public Card( Bitmap bitmap, LinearLayout linearLayout,Context context){
ImageView imageView=new ImageView(context);
imageView.setImageBitmap(bitmap);
imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
linearLayout.addView(imageView);
}
和
public class MainActivity extends AppCompatActivity {
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.jpg);
Card card;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout=findViewById(R.id.ll);
card=new Card(bitmap,linearLayout,this);
setContentView(R.layout.activity_main);
移动
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.jpg);
在 onCreate 方法中定义。 这应该解决 NullPointerException 因为上下文尚未定义。
所以它应该如下所示:
public class MainActivity extends AppCompatActivity {
Bitmap bitmap;
Card card;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.jpg);
LinearLayout linearLayout=findViewById(R.id.ll);
card=new Card(bitmap,linearLayout,this);
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.