【问题标题】:Android activity produces black screen because of the contentAndroid活动因内容而产生黑屏
【发布时间】:2014-09-04 08:50:41
【问题描述】:

我有 2 个活动。

ActivityA 通过单击按钮启动 ActivityB。

单击按钮时,我会创建一个新对话框并将图像显示为一种缩减的加载屏幕(不受进度条的困扰)。

当 ActivityB 从 ActivityA 启动对话框时,它被杀死,我立即在 oncreate 的顶部开始一个新的。我的 oncreate 为 activityB 做了很多事情。

创建一个广告视图,主视图的一些文本并创建一个 GLsurface 渲染器。

问题是,当调用 activityB 的 oncreate 时,我的两个对话框之间会出现 4 秒的黑屏。我想尽量减少这种情况。

所以无论如何都要让活动 B 中的对话框立即运行,而不是等待其余的 onCreate 完成它正在做的事情。需要的话会提供代码

更新。正是我的 glsurface 渲染器的创建导致了黑屏这么长时间。这是活动B代码

public class ActivityB extends BaseGameActivity
{
/** Hold a reference to our GLSurfaceView */
private MyGLSurfaceView mGLSurfaceView;
public GamePlay GamePlay;
public GoogleApiClient mGoogleApiClient = null;
private AdView adView;
private static final String AD_UNIT_ID = "******************************";
private Button RotateButton;

@Override
public void onCreate(Bundle savedInstanceState)
{       
    super.onCreate(savedInstanceState);
    // load screen dialog
     Dialog loader_dialog = new Dialog(this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        loader_dialog.setContentView(R.layout.loading_screen);
        loader_dialog.show();
    // Create an ad.
    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    RelativeLayout layout = new RelativeLayout(this);
    layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,    LayoutParams.MATCH_PARENT));

    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice("*****************************").build();
    // Start loading the ad in the background.
    adView.loadAd(adRequest);

    final TextView Score = new TextView(this);
    Score.setText(" 0");
    RelativeLayout.LayoutParams scoreParams = new 
    RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    Score.setLayoutParams(scoreParams);

    Typeface tf = Typeface.createFromAsset(getAssets(),"Fonts/test.ttf");
    Score.setTextSize(getResources().getDimension(R.dimen.textsize));
    Score.setTypeface(tf);
    Score.setTextColor(Color.parseColor("#FDAA03"));

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
    View userInterface = inflater.inflate(R.layout.user_interface, null);

    GameButton = (Button) userInterface.findViewById(R.id.gamebutton);

    GameButton.setOnTouchListener(new OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            //irrelivant game stuff

        }
    });

    // Check if the system supports OpenGL ES 2.0.
    final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
    final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;

    if (supportsEs2)
    {

        // Request an OpenGL ES 2.0 compatible context.
        mGLSurfaceView = new MyGLSurfaceView(this); new GLSurfaceView(this);
        mGLSurfaceView.setEGLContextClientVersion(2);           
        final DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);          
        // Set the renderer to our demo renderer, defined below.
        mGoogleApiClient = getApiClient();

        RelativeLayout.LayoutParams adParams = 
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        layout.addView(mGLSurfaceView);
        layout.addView(userInterface);
        layout.addView(Score);
        layout.addView(adView, adParams);

        mGLSurfaceView.setRenderer(new MyRenderer(this, Score,  mGoogleApiClient),displayMetrics);


        //Set main renderer             
        setContentView(layout); 


    }
    else
    {
        // This is where you could create an OpenGL ES 1.x compatible
        // renderer if you wanted to support both ES 1 and ES 2.
        return;
    }   

}   


@Override
protected void onResume() 
{
    // The activity must call the GL surface view's onResume() on activity onResume().
    super.onResume();
    mGLSurfaceView.onResume();
}

@Override
protected void onPause()
{
    // The activity must call the GL surface view's onPause() on activity onPause().
    super.onPause();
    mGLSurfaceView.onPause();
}

}

【问题讨论】:

  • 将显示对话框的代码放在onCrate 的顶部。如需进一步帮助,请发布一些代码。

标签: java android android-activity


【解决方案1】:

您可以为此使用AsyncTask

仅在onCreate 中准备 UI 元素(获取对所需视图的引用、显示对话框),但将所有繁重的任务移至 AsyncTask,并更新 UI(关闭对话框、填充视图、... ) 从那里在onPostExecute 加载完成后。如果需要,您甚至可以使用AsyncTask 来显示加载进度;)

【讨论】:

  • 我同意,您应该始终将 AsyncTask 用于长时间运行的任务,因为它是为这样的事情而设计的。
  • @2Dee 感谢您的回答,我已经用 activityB 代码更新了我的问题,问题是 GlsurfaceView 的创建,是否可以将其移动到 AsyncTask,记住不同传递给渲染器的参数?
【解决方案2】:

你可以用一个

public static Dialog d;

在活动 A 中。 在活动a中显示 转到活动 B 并且所有操作都成功完成 您可以从活动 B 中关闭该对话框。喜欢

if(A.d.isshowing()){
  A.d.dismiss();
}

希望这能解决您的问题..

【讨论】:

  • 感谢您的回答,但这似乎不起作用,在开始新任务时对话框似乎仍然被杀死,即使它是静态的并且我从不解雇()它
猜你喜欢
  • 2016-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多