【问题标题】:Android emulator rotates but app doesn't redrawAndroid模拟器旋转但应用程序不重绘
【发布时间】:2013-12-20 05:56:16
【问题描述】:

我在其他地方看到过这样的图片,但从前一段时间,答案通常是“这是 Android 2.3 的一个已知问题”,我使用的是 4.4,所以这绝对不是答案。

我有一个最简单的程序:“你好,Android”。当我启动模拟器时,它以纵向模式加载。使用 Fn-Ctrl-F11 (Mac),模拟器旋转到横向模式。但应用程序和手机控件不会重绘 - 整个画面只是横向显示。

这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.helloandroid"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="18"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.test.helloandroid.Hello"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

以及活动 XML 文件:

<LinearLayout 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"

tools:context=".Hello" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</LinearLayout>

我正在使用 Eclipse 构建,ADT 捆绑包构建 v22.3.0-887826,虽然我无法想象这对于这么微不足道的事情很重要。

我的模拟器适用于设备 Galaxy Nexus,Android 4.4 API 级别 19。我已经尝试过使用标记和未标记的硬件键盘。我发现了对“键盘盖支持”设置的引用,这是我在任何地方都没有看到的 - 此评论来自 3/12,因此可能已过时。

这是我的第一个 Android 应用程序,所以我完全是在这个环境中调试的新手。 TIA 对我遗漏的任何建议。

编辑:为 hello.java 添加代码

package com.test.helloandroid;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class Hello extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hello);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.hello, menu);
    return true;
}

}

【问题讨论】:

  • 当方向改变时活动被销毁并重新创建。
  • Hello.java的邮政编码
  • 发布你的Hello.java代码
  • @MehulJoisar 感谢您的链接 - 以及 Log 和 Toast 建议。我已经更新了我的代码,使其具有与您的建议相同的日志消息。有趣的是,当我第一次通过 Eclipse 启动应用程序时,我得到了 OnCreate - 但是当我旋转模拟器时,我没有收到 Toast 消息。

标签: android eclipse android-emulator autorotate


【解决方案1】:

如果你是初学者,你应该通过this reference document了解Activity lifecycle

在这里,我包括几个LogToast,以便更容易理解旋转屏幕时发生的过程。

示例:

package com.test.helloandroid;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class Hello extends Activity {


private static final String TAG = "Hello";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hello);

    Log.d(TAG, "onCreate");
    Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.hello, menu);
    return true;
}

@Override
protected void onResume() {
    super.onResume();

    Log.d(TAG, "onResume");
    Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
}

@Override
protected void onPause() {
    super.onPause();

    Log.d(TAG, "onPause");
    Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
}


}

希望对你有帮助!!

【讨论】:

  • 我期待当屏幕旋转时我会看到第二条消息出现,但实际上并没有发生。我赞成您的回答,因为它是非常有用的信息,但不幸的是,我在旋转的模拟器外壳内留下了非旋转内容。
【解决方案2】:

显然,一切旧的又是新的:orientation change bug in 4.4

“他们对管道的考虑越多,堵塞排水管就越容易。”

很高兴知道我没有遗漏一些明显的东西; OTOH,这对于谷歌的质量保证来说是一个非常明显的失败……比尔盖茨是在没人看的时候偷偷溜进去的吗?还是他们贪婪并试图通过破坏模拟器来出售手机进行测试?看来我的未来会有设备。

编辑:

参考:Android 框架工程师 CommonsWare 回答。

Impossible to rotate the emulator with android 4.4

【讨论】:

    猜你喜欢
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 2014-05-03
    • 1970-01-01
    • 2013-11-12
    • 2023-03-24
    相关资源
    最近更新 更多