java - 从另一个 Activity 中调用方法

标签 java android android-activity

我知道这个问题之前被问过,但我已经尝试了所有解决方案并得到错误

我在 android studio 上有两项 Activity ...

第一个名为“MainActivity”并包含一个方法“deleteFromArrayList()” 第二个名为“DeletButtonActivity”并包含一个方法“delete(View v)”

我想调用“deleteFromArrayList ()”而不创建另一个类或使该方法静态......因为我在deleteFromArrayList()中有一个ArrayList

注意:我发送要使用 Intent 从数组列表中删除的索引值。DeleteButtonActivity 中的代码是 公共(public)无效删除(查看v){

    try {

        Intent i = new Intent(DeleteButton.this, MainActivity.class);
        i.putExtra("index", (int) spinner2.getSelectedItemId());

        (new MainActivity()).DeletButtonActivity();

        Toast.makeText(getApplicationContext(), "it was deleted", Toast.LENGTH_SHORT).show();



    }
    catch(Exception e){
        Toast.makeText(getApplicationContext(), e+"", Toast.LENGTH_SHORT).show();


    }


}

以及MainActivity中的代码

  public void deleteFromArrayList (){

    this.arrayList.remove(getIntent().getIntExtra("index",-1));

 }

当我运行应用程序时,我收到错误 NullPointerException,,, 谁能帮助我..拜托 希望我能很好地描述问题

最佳答案

Android 中的 Activity 不仅仅是一个简单的类,它们还有一个 Lifecycle :

An activity has essentially four states:

If an activity is in the foreground of the screen (at the top of the stack), it is active or running. If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused.

A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.

If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.

If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

因此,您的代码存在的问题是,当您想要在已停止的 Activity 中访问数组时,您之前使用的实例可能不再 Activity 了。

如何解决您的问题

一种非常简单的方法是在从一个 Activity 转换到另一个 Activity 之前使用参数传递,为此,您可以将数组作为 intent extra 传递。然后当您使用 onActivityResult() callback 完成第二个 Activity 时“返回结果” .

第二种方法可能是使用 Service这与 Activity 类似,但它没有 UI 并且有自己的生命周期。即使你应用它不是,也能够活着。使用服务,您可以将数组保留在服务内,并且您将 communicate使用数组进行通常的操作。

第三种方法可能是使用 EventBus 。 Activity 、 fragment 、线程、服务之间非常简单的通信机制。有一个很棒的演讲,标题为Android Application Architecture在 Android Dev Summit 2015 上,使用 EventBus 作为通信机制并在 REST Android 应用程序上实现 MVC 架构模式。

回到你的问题。如果您只需要在两个 Activity 之间“共享”数组,请使用第一种方法。第二个和第三个只是针对您需要的更多的情况的不同替代方案的示例。

关于java - 从另一个 Activity 中调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39058769/

相关文章:

java - 如何在最近屏幕后返回到上一个 Activity

java - Android源代码如何缩小和混淆?

java - 如何对对象集合进行排序?

java - 如何检查另一个Jframe中的按钮是否被单击

android - Marshmallow FINE 和 COARSE 位置许可

Android Kotlin 检测测试是否在调用 finish() 开始另一个 Activity 后结束/完成一个 Activity

java - Hadoop - 如何从 mapred.JobConf 中提取 taskId?

android - 如何从android库回调到各种 Activity

java - SimpleDateFormat 对同一日期字符串的解析和格式化给出了不同的结果

java - Fragment 返回导航退出到主屏幕 - Android