Android 创建没有静态的可共享资源绘图必须是可能的我缺少什么?
我在一个项目中走得很远,遇到了一个不受欢迎的障碍,无法找到解决方案。我有一些sudo代码,我相信它们会说明我正在尝试做什么,但不是完整的代码。尝试将其传递给ImageHandler时,主类中ContextWrapper上的错误为NullPointerException。不幸的是,ImageView和我怀疑所有视图都试图在活动调用onCreate之前解析它们的可绘制内容。因此,我必须尝试将图像存储到活动构造函数中,但无法传递上下文。我已经考虑过AsyncTask或处理程序等待活动的onCreate(),但我还是回到SurfaceView并创建自己的设计。我会继续寻找,但如果有人有更多的安卓经验,那么我知道更好的设计或解决方案的帮助将是巨大的。此外,高度推荐使用兼容的eclipse布局预览器。我还尝试让每个imageview独立地拥有所需的LayerDrawable,尽管它们中的很多都是相同的,但性能很差。必须在活动中添加构造函数,因为如果imagehandler是在onCreate中创建的,则当ImageView尝试在其构造函数上调用它时,它将为null。为什么Activities组件在调用Activity.onCreate之前就准备好了,我还不太明白Android 创建没有静态的可共享资源绘图必须是可能的我缺少什么?,android,android-intent,android-activity,android-context,Android,Android Intent,Android Activity,Android Context,我在一个项目中走得很远,遇到了一个不受欢迎的障碍,无法找到解决方案。我有一些sudo代码,我相信它们会说明我正在尝试做什么,但不是完整的代码。尝试将其传递给ImageHandler时,主类中ContextWrapper上的错误为NullPointerException。不幸的是,ImageView和我怀疑所有视图都试图在活动调用onCreate之前解析它们的可绘制内容。因此,我必须尝试将图像存储到活动构造函数中,但无法传递上下文。我已经考虑过AsyncTask或处理程序等待活动的onCreate
public class MainActivity extends Activity {
ImageHandler ih;
MainActivity(){
Log.d("Acitivity", "Constructed");
ih=new ImageHandler(this);
//ih=new ImageHandler(this.getApplicationContext());
//ih=new ImageHandler(this.getBaseContext());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Activity", "Created");
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public LayerDrawable getImage(int some_identifier){
return ih.getImage(some_identifier);
}
}
===================
public class ImageHandler {
LayerDrawable[] image;
ImageHandler(Context context){
Log.d("ImageView", "Constructed");
//manipulate resource images to create custom layerdrawables and store.
//originally attempted to create a layer.xml and although it worked
//in the emulator perfectly the behavior was quite different in live tests.
//although it made sense any change to layer.xml images was global for all who used it
//the strange behavior was that the emulator behaved as desired.
}
public LayerDrawable getImage(int identifier){
return image[identifier];
}
}
public class SomeImageView extends ImageView{
public SomeImageView(Context context, AttributeSet attrs) {
super(context, attrs);
int some_identifier=0;
this.setImageDrawable(((MainActivity)context).getImage(some_identifier));
}
}
====================
public class ImageHandler {
LayerDrawable[] image;
ImageHandler(Context context){
Log.d("ImageView", "Constructed");
//manipulate resource images to create custom layerdrawables and store.
//originally attempted to create a layer.xml and although it worked
//in the emulator perfectly the behavior was quite different in live tests.
//although it made sense any change to layer.xml images was global for all who used it
//the strange behavior was that the emulator behaved as desired.
}
public LayerDrawable getImage(int identifier){
return image[identifier];
}
}
public class SomeImageView extends ImageView{
public SomeImageView(Context context, AttributeSet attrs) {
super(context, attrs);
int some_identifier=0;
this.setImageDrawable(((MainActivity)context).getImage(some_identifier));
}
}
再次感谢社区提供的帮助。为什么您有MainActivity的构造函数在创建
ih=new ImageHandler(此)时将其删除并移动到内部代码>当我这样做时,错误是ImageView中的NullPointer,因为ImageView在创建活动之前解析其构造函数。从何处调用SomeImageView
构造函数?不应创建活动类的构造函数。在布局中调用了SomeImageView。我进行了编辑,以显示活动和imageview都已构建,然后创建了活动。希望能够在Eclipse布局图形界面中使用CustomLayoutView选项卡。图像标识符与attr.xml中的自定义资源属性一起传递。活动
的上下文
从onCreate()
方法开始有效,因此在onCreate()
方法中创建/初始化ImageHandler
对象,然后设置布局的内容视图。不要保存对可绘图项的静态引用。