Android - 横向和纵​​向模式之间切换使 Intent 丢失值

发布于 2024-07-13 23:36:14 字数 445 浏览 6 评论 0原文

我正在使用 Intents 在我的 Android 应用程序中的活动之间切换。 我将数据放入 Intent 中以供下一个活动使用。 当我在横向和纵向模式之间切换手机时,从意图传递的值会丢失,并且会出现 NullPointerException。

有人可以告诉我可能出了什么问题吗?

有很多代码可以完整发布。 但如果有人需要查看代码的特定部分,我可以将其发布在这里。

编辑
我解决了状态未保存的问题。 但我面临的另一个问题是,方向改变后,屏幕上的按钮都不起作用。 按下按钮时,我在 LogCat

02-25 23:07:49.190: WARN/WindowManager(58): No window to dispatch pointer action 0

请帮助中收到此警告。

I am using Intents to switch between activities in my Android app. I am putting data in the Intent for use in the next activity. When I switch the phone between landscape and portrait modes, the values passed from the intent are lost and I get a NullPointerException.

Can someone please tell me what could be wrong.

There's a lot of code to post it entirely. But if someone needs to look at specific parts of code, I can post it here.

Edit
I solved the issue of state not being saved. But another problem I faced is that none of the buttons on the screen work after the orientation has been changed. On button press, I get this warning in LogCat

02-25 23:07:49.190: WARN/WindowManager(58): No window to dispatch pointer action 0

Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

寒冷纷飞旳雪 2024-07-20 23:36:14

当您切换方向时,将重新创建 Activity 并调用 onCreate,因此您必须使用捆绑包来保存当前状态并在方向更改后恢复。 如果您只有一个带有 TextView 的应用程序并且输入文本并更改方向,则可以看到此操作的实际效果。 如果将 onCreate 的状态捆绑起来,就可以遏制这种情况。 这可能也是方向改变后出现 NullPointer 的原因。 这真是太烦人了,但我们必须忍受。

链接提供一系列方向教程和第一个 尤其应该帮助您准确了解正在发生的情况以及如何成功维持当前状态。

更新:还有一篇关于 SO Activity restart on Rotation Android 的帖子,涉及几乎一样的东西。

编辑后续问题:

方向更改后您是否重新附加了点击处理程序?

When you switch orientation the activity is recreated and onCreate is recalled so you have to use the bundle to save your current state and restore after an orientation change. You can see this in action if you have just an app with a TextView and you enter text and change orientation. If you bundle your state for onCreate you can curb this. This is probably also why you have a NullPointer after the orientation changes. It is annoying as all hell but something we have to live with.

This link on a series of orientation tutorials and this first one in particular should help you understand exactly what is going on and how to successfully maintain your current state.

Update: There is also a post on SO Activity restart on rotation Android that deals with almost the same thing.

Edit for follow up question:

Did you re-attach your click handlers after the orientation change?

能否归途做我良人 2024-07-20 23:36:14

将其写入您的清单文件中..在您想要的活动中 -

 android:configChanges="orientation|keyboardHidden"

已编辑:将此用于新的 API 版本 -

android:configChanges="orientation|keyboardHidden|screenSize"

绝对会起作用..

Write this in your manifest file..in which activity you want this--

 android:configChanges="orientation|keyboardHidden"

Edited: Use this one for new APIs versions--

android:configChanges="orientation|keyboardHidden|screenSize"

Definitely it will work..

树深时见影 2024-07-20 23:36:14

尝试这个:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString(SOME_KEY, "blah blah blah");
}

@Override
public void onCreate(Bundle savedInstanceState) {
   ...
   somevalue = savedInstanceState.getString(SOME_KEY);
   ...
}

Try this:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString(SOME_KEY, "blah blah blah");
}

@Override
public void onCreate(Bundle savedInstanceState) {
   ...
   somevalue = savedInstanceState.getString(SOME_KEY);
   ...
}
夏了南城 2024-07-20 23:36:14

可以声明一个值为“orientation”的属性 android:configChanges,这将阻止 Activity 重新启动。 相反,该活动保持运行状态并调用其 onConfigurationChanged() 方法。

It possible to declare an attribute android:configChanges with the value of "orientation", this will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.

断舍离 2024-07-20 23:36:14

声明 < android:configChanges="orientation|keyboardHidden"/> 在你的清单中。 这使您可以自行管理方向/键盘可见性的更改。 当然,您不需要重写回调方法来管理它。

Declare < android:configChanges="orientation|keyboardHidden"/> in your manifest. This allows you manage the change of Orientation/Keyboard visibility by yourself. Of course, You don't need to override the callback method for manage it.

信仰 2024-07-20 23:36:14

你好我也遇到了这个问题。
为我解决的问题是:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putString("Username", mUsername);
    savedInstanceState.putString("Password", mPassword);
    savedInstanceState.putString("UserID", mUserID);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

然后在 onCreate(): 中,

if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        mUsername = "?";
        mPassword = "?";
        mUserID = "?";
    } else {
        mUsername = extras.getString("Username");
        mPassword = extras.getString("Password");
        mUserID = extras.getString("UserID");
    }
} else {
    mUsername = (String) savedInstanceState.getSerializable("Username");
    mPassword = (String) savedInstanceState.getSerializable("Password");
    mUserID = (String) savedInstanceState.getSerializable("UserID");
}

然后您可以确定对象不为空。

Hi I also encountered this problem.
what fixed it for me was:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putString("Username", mUsername);
    savedInstanceState.putString("Password", mPassword);
    savedInstanceState.putString("UserID", mUserID);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

and then in onCreate():

if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        mUsername = "?";
        mPassword = "?";
        mUserID = "?";
    } else {
        mUsername = extras.getString("Username");
        mPassword = extras.getString("Password");
        mUserID = extras.getString("UserID");
    }
} else {
    mUsername = (String) savedInstanceState.getSerializable("Username");
    mPassword = (String) savedInstanceState.getSerializable("Password");
    mUserID = (String) savedInstanceState.getSerializable("UserID");
}

then you can be sure the objects are not null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文