Android应用程序在重复启动和退出后显示空白屏幕

时间:2015-03-25 09:15:41

标签: android android-layout android-activity

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

在从另一个活动启动的活动的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()之前获取日志(这意味着活动正在正常启动)但有时,即使在获取日志之后,也没有显示布局。 空白屏幕不是每次都会出现,但有时候。知道为什么会这样,以及如何解决它?如果需要进一步的信息,请告诉我。

2 个答案:

答案 0 :(得分:0)

你为什么不尝试这样

 @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); 

}

答案 1 :(得分:0)

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

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

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

相关问题