ImageView不会在Android中显示位图

时间:2016-07-25 12:36:04

标签: android bitmap imageview

我们想在ImageView中放置位图(setView中的元素)并显示它。当我们运行应用程序时,我们没有任何错误,只是一个黑色的背景。

 public void setView(View element) {
    mainView = element;
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
    mainView.setVisibility(View.VISIBLE);
    setContentView(element);
    try {
        rootLayout.addView(mainView,layoutParams);
    }catch (Exception e){
        e.printStackTrace();
    }
}

这是我们的 onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    this.getInstance();
    this.authService = new AuthenticationService();
    this.authService.getInstance(this);

    this.settingsManager = SettingsManager.getInstance();
    this.statusManager = StatusManager.getInstance();
    super.onCreate(savedInstanceState);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    mainView = findViewById(R.id.mainContentView);
    loadingView = null;
    mInflater = (LayoutInflater) AuthenticationService.getContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    vs = (View) mInflater.inflate(R.layout.activity_main, null);
    rootLayout = (RelativeLayout) vs.findViewById(R.id.rootLayout);

    AsyncCallRegistrationWS task = new AsyncCallRegistrationWS();
    task.execute();
}

我们已尝试在stackoverflow上解决此问题的所有建议解决方案,但对我们没有任何作用。

编辑:

我们在另一个Class

中调用setView()
public void createDisplay(){
    creationDate = DateTime.now();
    try {
        if (state == DisplayState.Validated){
            if (entry != null && entry.template != null){
                DateTime now = DateTime.now();

                if (SettingsManager.getInstance().playlistTimeOffset != null){
                    now.plus(SettingsManager.getInstance().playlistTimeOffset);
                }
            }
            WindowManager wm = (WindowManager) AuthenticationService.getContext().getSystemService(Context.WINDOW_SERVICE);
            android.view.Display screenDisplay = wm.getDefaultDisplay();
            int width = screenDisplay.getWidth();
            int height = screenDisplay.getHeight();

            View element = create(width,height);
            if (element != null){
                StartUpActivity.getInstance().setView(element);

                state = DisplayState.Created;
                if (contentState == DisplayContentState.Unknown){
                    contentState = DisplayContentState.Valid;
                }
            } else {
                destroyDisplayOnFail();
            }
        }
    } catch (Exception e){
        Log.e(LOG_TAG, "Creating display failed. "+getLoggingContentIdentificationString()+" "+e.toString());
        destroyDisplayOnFail();
    }
}

1 个答案:

答案 0 :(得分:0)

不要使用setContentView(element);在setView方法中,因为您替换了活动视图。您的rootLayout是R.layout.activity_main视图的子视图,它被替换。这就是你什么都看不见的原因。