简体   繁体   English

单例活动类getInstance()返回Null

[英]Singleton Activity Class getInstance() Returns Null

I have an activity like: 我有一个类似的活动:

public class ListActivity extends ActionBarActivity {
    private static ListActivity instance;

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

        Log.i("List", "Instance Created");
        instance = this;
        Log.i("List", (instance == null) ? "null" : "not null");
    }

    public static ListActivity getInstance() {
        return instance;
    }
}

When the app starts, LogCat will report: 应用启动时,LogCat将报告: Never mind: 没关系:

I/List﹕ Instance Created
I/List﹕ not null

But later on in my other activity, 但是后来在我的其他活动中,

startActivity(new Intent(MainActivity.getInstance(), ListActivity.class));
ListActivity.getInstance().updateList(files);

will produce a null pointer exception for trying to access updateList() on a "null object reference". 将产生空指针异常,以尝试访问“空对象引用”上的updateList() I also threw in a Log.i() right before the 2nd line to confirm that it's indeed a null... 我还在第二行之前添加了一个Log.i() ,以确认它确实为空。

Can somebody explain why? 有人可以解释为什么吗?

Edit 1. Watered down version of MainActivity ( onCreate() not watered down): 编辑1.淡化的MainActivity版本( onCreate()未淡化):

public class MainActivity extends ActionBarActivity {
    private static MainActivity instance;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        instance = this;
    }

    public static MainActivity getInstance() {
        return instance;
    }
}

First off- never do this. 首先,千万不要这样做。 Its a memory leak. 它是内存泄漏。 Never hold a static reference to an Activity or Context. 永远不要持有对活动或上下文的静态引用。 If you need to update data in it, that data may be appropriate for a singleton, but an Activity NEVER is. 如果您需要更新其中的数据,则该数据可能适合单身人士,但“活动”永远不会。

Secondly- are you sure that the list activity was ever shown? 其次-您确定曾经显示过列表活动吗? If not then the variable would be null. 如果不是,则该变量将为null。 That also includes the case of the app being killed and later relaunched from the saved intent in the recents menu. 这还包括该应用程序被杀死,然后又从“最近”菜单中保存的意图中重新启动的情况。

Although this is not a memory leak, this is definitely not a correct android design pattern. 尽管这不是内存泄漏,但这绝对不是正确的android设计模式。 The problem is you are assuming that when you pass the line: 问题是您假设通过该行时:

startActivity(new Intent(MainActivity.getInstance(), ListActivity.class));

your activity has been created. 您的活动已创建。 THIS IS WRONG! 这是错误的!

In other words, running startActivity method does not INCLUDE running onCreate method of the activity that is supposed to run. 换句话说,运行startActivity方法不包括应该运行的活动的运行onCreate方法。

The above line only sends a message to the main looper to run the next activity at a later time (possibly a few msec). 上一行仅向主循环程序发送一条消息,以在以后的时间(可能是几毫秒)运行下一个活动。


Again: this is not a memory leak - but it is definitely a poor way of managing memory . 再说一次: 这不是内存泄漏 -但这绝对不是管理内存糟糕方法 By keeping a reference to your activity you keep your UI components in the memory. 通过保留对活动的引用,可以将UI组件保留在内存中。 You need look into a more appropriate design pattern to satisfy your need. 您需要研究更合适的设计模式以满足您的需求。

One possible way to have a flag in an external singleton with some THINGS_TO_BE_DONE_UPON_CREATING_LIST_VIEW data. 一种在外部单例中带有一些THINGS_TO_BE_DONE_UPON_CREATING_LIST_VIEW数据的标志的可能方法。 And check these values in your onCreate and update your list and etc according to this information rather than keeping a static reference to the activity itself. 并根据此信息在onCreate中检查这些值并更新列表等,而不是对活动本身进行静态引用。 This is very crude example: 这是非常粗糙的示例:

Class MySingleton{
    public static String[] files;
}

in your ListView : 在您的ListView

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    if (MySingleton.files!=null)
       updateList(MySingleton.files);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM