android - 在另一个 Activity 开始后单击 Listview 按钮完成或销毁 Activity

标签 android baseadapter activity-finish getview

我有一个具有 Listview 的 Activity ListActivity,另一个类 CustomListAdapter 扩展了 BaseAdapter。

ListActivity 中的代码

customAdapter = new CustomListAdapter(list);
TripList.setAdapter(customAdapter);

在 CustomListAdapter 的 getView() 中,我膨胀了一个布局。有一个按钮,单击该按钮我将开始另一项 Activity 。

我想在另一个 Activity 开始后完成 ListActivity。 使用以下代码我的应用程序崩溃了。

((Activity) ctx).finish();

日志是

01-05 11:06:04.319: E/AndroidRuntime(4319): FATAL EXCEPTION: main
01-05 11:06:04.319: E/AndroidRuntime(4319): Process: com.example.myapp, PID: 4319
01-05 11:06:04.319: E/AndroidRuntime(4319): java.lang.NullPointerException
01-05 11:06:04.319: E/AndroidRuntime(4319):     at com.example.myapp.CustomListAdapter$1.onClick(CustomListAdapter.java:71)

请帮帮我。

最佳答案

首先,您可以发布您的 Logcat

FLAG_ACTIVITY_CLEAR_TOP

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

FLAG_ACTIVITY_NEW_TASK

if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state.

您可以使用Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK

最后,应该是,

   Intent intent = new Intent(ctx, NewActivityName.Class);
   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK);
   intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
   startActivity(intent);

关于android - 在另一个 Activity 开始后单击 Listview 按钮完成或销毁 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34607021/

相关文章:

java - 登录认证编程模式

android - 简单的空白 Activity 会导致内存泄漏。为什么?

java - 每行具有不同数量 TextView 的 ListView

android - 具有多种项目类型的 ListAdapter 抛出 ClassCastException

android - finish() 关闭应用程序而不是 Activity

android - finish() 和 System.exit(0) 之间的区别

android - 恢复 fragment 中的 EditText 输入

java - AVD 没有 LogCat 输出?

android - 订阅多个 Observable 的适配器

java - Android:无法调用 finish()