从 Air Native Extension 显示 ImageView 时出现问题 (android)

Issue displaying ImageView from Air Native Extension (android)

首先我想说的是我是 java 和 android 的初学者,我花了很多时间尝试完成这项工作并寻找答案,但我无能为力.

我正在开发 Air Native Extension。如果现在我成功创建了这个 ANE 并使大部分调用正常工作,那么在从这个 ANE 显示 ImageView 时,我就被卡住了。

似乎我需要知道一些东西才能正确显示 ImageView,所以在启动时我检索了一些信息,例如 activity、上下文和 rootView。

public class MyContext extends FREContext 
{

    @Override
    public void dispose() 
    {

    }


    @Override
    public Map<String, FREFunction> getFunctions()
    {
        Configs.activity    = this.getActivity();
        Configs.context     = Configs.activity.getApplicationContext();
        Configs.decorView   = Configs.activity.getWindow().getDecorView();

        Map<String, FREFunction> map = new HashMap<String, FREFunction>();

        map.put("init"                  , new InitFunction());
        map.put("oneFunction"           , new myFunction());

        return map;
    }

}

然后我调用这个 class 从网络加载图像,加载后我想在屏幕上显示它

public class GraphicLoader extends AsyncTask<URL, String, String>
{
    public ImageView    view;

    private URL         url;
    private int         width;
    private int         height;


    // Constructor
    public GraphicLoader (String url, int width, int height)
    {
        this.width  = width;
        this.height = height;

        try 
        {
            this.url    = new URL (url);
        } 
        catch (MalformedURLException e) 
        {
            e.printStackTrace();
        }
    }


    @Override
    protected String doInBackground(URL... params) 
    {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;

        try 
        {
            this.view   = new ImageView (Configs.context);
            view.setImageBitmap(BitmapFactory.decodeStream(url.openConnection().getInputStream(), null, options));

            RelativeLayout relativeLayout = new RelativeLayout(Configs.context);

            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT);

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
            view.setLayoutParams(lp);

            relativeLayout.addView(view);
            Configs.activity.setContentView(relativeLayout, rlp);

        } 
        catch (IOException e) 
        {
            Configs.debug = Configs.debug + " GraphicLoaded error 2 " + e.getMessage();
            e.printStackTrace();
        }

        return null;
    }
}

我的应用程序在进入 doInBackground 函数时崩溃,在第 "Configs.activity.setContentView(relativeLayout, rlp);" 行。我可能没有以正确的方式使用它,我尝试了多种方法来做到这一点,具体取决于我在网上找到的内容,但没有成功并且总是变成崩溃。

应该怎么做?

感谢您的帮助?

所以我找到了解决方案,只需将这部分代码移出 AsyncTask 即可:

RelativeLayout relativeLayout = new RelativeLayout(Configs.context);

            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT);

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
            view.setLayoutParams(lp);

            relativeLayout.addView(view);
            Configs.activity.setContentView(relativeLayout, rlp);

应用程序不会再崩溃,并且会正确显示 ImageView。