0

我在一个项目中走得很远,遇到了一个不受欢迎的障碍,无法找到解决方案。我有一些 sudo 代码,我相信它们可以说明我正在尝试做的事情,但不是完整的代码。尝试将 Main 类中的 ContextWrapper 传递给 ImageHandler 时,错误是 NullPointerException。不幸的是,ImageViews 和我怀疑所有的 Views 都尝试在活动调用 onCreate 之前解析它们的可绘制对象。所以我不得不尝试将图像存储到 Activity 构造函数中,但是我无法传递上下文。我考虑过 AsyncTask 或处理程序来等待活动的 onCreate() ,但我不妨回到 SurfaceView 并创建自己的设计。我会继续看,但如果有人有更多的 android 经验,那么我知道更好的设计或解决方案,帮助将是巨大的。此外,兼容的 Eclipse 布局预览器也是非常受欢迎的。我还尝试让每个图像视图独立地具有所需的 layerdrawables,即使它们中的很多是相同的,但性能很糟糕。必须在 Activity 中添加构造函数,因为如果在 onCreate 中创建图像处理程序,当 ImageView 尝试在其构造函数上调用它时,它将为空。为什么活动组件在调用 Activity.onCreate 之前准备好自己,我还不太明白。我还尝试让每个图像视图独立地具有所需的 layerdrawables,即使它们中的很多是相同的,但性能很糟糕。必须在 Activity 中添加构造函数,因为如果在 onCreate 中创建图像处理程序,当 ImageView 尝试在其构造函数上调用它时,它将为空。为什么活动组件在调用 Activity.onCreate 之前准备好自己,我还不太明白。我还尝试让每个图像视图独立地具有所需的 layerdrawables,即使它们中的很多是相同的,但性能很糟糕。必须在 Activity 中添加构造函数,因为如果在 onCreate 中创建图像处理程序,当 ImageView 尝试在其构造函数上调用它时,它将为空。为什么活动组件在调用 Activity.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));
}

 }

再次感谢社区的任何帮助。

4

0 回答 0