意图不提取额外的

发布于 2024-09-13 08:44:38 字数 748 浏览 3 评论 0原文

我有这个代码:

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Log.i(TAG, "The id of the selected note is " + id);
    Intent editNote = new Intent(this, TaskEditActivity.class);
    editNote.putExtra(TasksDBAdapter.KEY_ID, id);
    startActivityForResult(editNote, EDIT_TASK_REQUEST);
}

这个代码检索额外的 FROM A DIFFERENT ACTIVITY

 if (savedInstanceState != null) {
        id = savedInstanceState.getLong(TasksDBAdapter.KEY_ID);
    }
 Log.i(TAG, "Id of note = " + id);

在第一个代码片段中,Logcat 说:所选注释的 id 是2,但在第二个代码片段中,Logcat 表示:Id of note = 0。这里刚刚发生了什么?对于这个非常烦人的问题的任何解决方案。

I have this code:

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Log.i(TAG, "The id of the selected note is " + id);
    Intent editNote = new Intent(this, TaskEditActivity.class);
    editNote.putExtra(TasksDBAdapter.KEY_ID, id);
    startActivityForResult(editNote, EDIT_TASK_REQUEST);
}

And this code that retrieves the extra FROM A DIFFERENT ACTIVITY:

 if (savedInstanceState != null) {
        id = savedInstanceState.getLong(TasksDBAdapter.KEY_ID);
    }
 Log.i(TAG, "Id of note = " + id);

In the first code snippet, Logcat says: The id of the selected note is 2, but in the second code snippet, Logcat says: Id of note = 0. What just happened here? Any solutions to this VERY annoying problem.

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

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

发布评论

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

评论(2

诺曦 2024-09-20 08:44:38

我认为您混淆了 Activity 暂停时保存的状态以及通过 Intent 传递到 Activity 的数据。

您想要这样的东西:

Bundle extras = getIntent().getExtras();
id = extras.getLong(TasksDBAdapter.KEY_ID);

传递给 onCreate()Bundle 是您使用 onSaveInstanceState() 方法 并不是您添加到 Intent 中的额外 Bundle

I think you're confusing the state which is saved when an Activity is paused and the data delivered to the Activity via an Intent.

You want to have something like:

Bundle extras = getIntent().getExtras();
id = extras.getLong(TasksDBAdapter.KEY_ID);

The Bundle passed to onCreate() is the Bundle you saved with the onSaveInstanceState() method and is not the extras Bundle you added to your Intent.

記柔刀 2024-09-20 08:44:38

您正在以非常错误的方式检索额外的内容。将第二个代码段替换为:

id = getIntent().getLongExtra(TasksDBAdapter.KEY_ID, 0);
Log.i(TAG, "Id of note = " + id);

以下是此代码中发生的情况: getIntent() 返回您在第一个代码段中创建的 IntentIntent > 用于启动当前活动)。然后,.getLongExtra() 返回附加的额外信息。如果没有找到带有该标签和该数据类型(long)的额外信息,它将返回0。

savedInstanceState用于在内存不足的情况下被Android系统关闭时保存应用程序的状态。不要混淆这两者。

You are retrieving the extra in a very wrong way. Replace your second code snippet with:

id = getIntent().getLongExtra(TasksDBAdapter.KEY_ID, 0);
Log.i(TAG, "Id of note = " + id);

Here's what happens in this code: getIntent() returns the Intent you created in your first code snippet (the Intent that was used to launch the current activity). Then, .getLongExtra() returns the attached extra information. If no extra information with that tag and that data type (long) is found, it will return 0.

savedInstanceState is used for saving your app's state when it is shut down by the Android system under low memory conditions. Don't confuse these two.

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