【问题标题】:Android App showing blank screen after repeated launch and exitAndroid App反复启动退出后显示黑屏
【发布时间】:2015-03-25 09:15:41
【问题描述】:

我正在编写一个从另一个活动启动的活动。如果反复退出并重新启动屏幕,启动的新活动有时会显示空白屏幕

在从另一个活动启动的活动的 onCreate() 之后:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    if (!isTaskRoot()) {
        final Intent intent = getIntent();
        final String intentAction = intent.getAction(); 
        if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
                Log.w("onCreate()", "Main Activity is not the root.  Finishing Main Activity instead of launching.");
            finish();
            return;       
        }
    }
    super.onCreate(savedInstanceState);
    Log.d("onCreate()", "Calling setContentView()");
    setContentView(R.layout.dial_screen);
    name = (TextView) findViewById(R.id.name);
    number = (TextView) findViewById(R.id.number);
    duration = (TextView) findViewById(R.id.duration);
    mute = (Button) findViewById(R.id.mute);
    end = (ImageButton) findViewById(R.id.end); 

}

这里是 dial_screen.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/bg1024_600"
tools:context="com.harman.contacts.CallScreenActivity" >

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="100dp"
    android:text="@string/hello_world"
    android:textSize="45sp"
    android:textColor="@android:color/white" />
<TextView
    android:id="@+id/number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/name"
    android:layout_marginTop="50dp"
    android:text="@string/hello_world"
    android:textSize="35sp"
    android:textColor="@android:color/white" />
<TextView
    android:id="@+id/duration"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/number"
    android:layout_marginTop="50dp"
    android:text="@string/dialling"
    android:textSize="35sp"
    android:textColor="@android:color/white" />

<LinearLayout 
    android:id="@+id/buttons"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/duration"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="80dp">

    <Button 
        android:id="@+id/mute"
        android:layout_width="450dp"
        android:layout_height="110dp"
        android:text="@string/mute"
        android:textSize="30sp"
        android:textColor="@android:color/white"
        android:layout_gravity="bottom"/>
    <ImageButton 
        android:id="@+id/end"
        android:layout_width="450dp"
        android:layout_height="100dp"
        android:layout_marginStart="30dp"
        android:background="@drawable/end_call"
        android:contentDescription="@string/hello_world"
        android:layout_gravity="bottom"/>
</LinearLayout>

每次我在 setContentView() 之前获取日志(这意味着活动正在正确启动)但有时,即使在获取日志之后,布局也不会显示。 空白屏幕不是每次都会出现,但有时会出现。知道为什么会这样以及如何解决吗?如果需要更多信息,请告诉我。

【问题讨论】:

  • 请告诉我为什么是'-1'。在发布问题之前,我已经在网上进行了足够的搜索。在 Android 默认呼叫应用程序中也观察到了类似的错误。但是,经过大量搜索后,我找不到任何东西。非常感谢您的帮助。
  • 我也得到了类似的东西。按返回退出时,我会得到一个空白的黑屏一段时间。我必须不断反复按下才能使应用程序完全退出:-(

标签: android android-layout android-activity


【解决方案1】:

你为什么不这样尝试

 @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("onCreate()", "Calling setContentView()");
    setContentView(R.layout.dial_screen);
    if (!isTaskRoot()) {
        final Intent intent = getIntent();
        final String intentAction = intent.getAction(); 
        if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
                Log.w("onCreate()", "Main Activity is not the root.  Finishing Main Activity instead of launching.");
            finish();
            return;       
        }
    }

    name = (TextView) findViewById(R.id.name);
    number = (TextView) findViewById(R.id.number);
    duration = (TextView) findViewById(R.id.duration);
    mute = (Button) findViewById(R.id.mute);
    end = (ImageButton) findViewById(R.id.end); 

}

【讨论】:

  • 这没有帮助。尽管如此,在连续按返回键并重新启动 Activity 后,我偶尔会出现空白屏幕。
【解决方案2】:

仍然不确定出了什么问题,但是当我按下 home 键时,我观察到这个问题没有发生,我找到了一个解决方法。我通过实现 onBackPressed() 使返回键的行为与返回键相同。希望这个黑客可以帮助面临类似问题的人:

public void onBackPressed() {
    moveTaskToBack(true);
}

这不是解决方案,而是一个简单的解决方法。我会将这个问题保留几天,以便有人提出实际的解决方案

【讨论】:

    猜你喜欢
    • 2019-08-22
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 2021-05-26
    • 2012-08-06
    相关资源
    最近更新 更多