【问题标题】:Issue displaying ImageView from Air Native Extension (android)从 Air Native Extension (android) 显示 ImageView 的问题
【发布时间】:2015-03-25 20:09:27
【问题描述】:

首先我想说我是 java 和 android 的初学者,我花了很多时间试图完成这项工作并寻找答案,但无法自拔。

我正在开发 Air Native 扩展。如果现在我成功地创建了这个 ANE 并让大部分调用正常工作,那么在显示来自这个 ANE 的 ImageView 时,我会被卡住。

看来我需要知道一些东西才能正确显示 ImageView,所以在启动时我会检索一些信息,例如活动、上下文和 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;
    }

}

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

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);”行.我可能没有以正确的方式使用它,我尝试了多种方式来做到这一点,具体取决于我在网上找到的内容,但没有任何成功并且总是导致崩溃。

应该怎么做?

感谢您的帮助?

【问题讨论】:

    标签: java android air


    【解决方案1】:

    所以我找到了解决方案,只需将这部分代码移出 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。

    【讨论】:

      猜你喜欢
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      相关资源
      最近更新 更多