简体   繁体   English

Android线程有时无法启动

[英]Android thread sometimes doesn't start

So I'm having problems with my threads in an Android project. 所以我在一个Android项目中遇到了线程问题。 I have a ThreadStarter class, with a BuildScreen() function, which actually creates the layout for each activity. 我有一个带有BuildScreen()函数的ThreadStarter类,它实际上为每个活动创建布局。 The only problem is, sometimes the threads just won't start, and I have no idea why. 唯一的问题是,有时线程不会启动,我也不知道为什么。 They work like 98% of the time though, but when they don't, the current activity will never get initalized, and the user has to restart the app, which is inconvenient. 尽管它们在98%的时间内都能正常工作,但是如果不这样做,当前活动将永远不会启动,并且用户必须重新启动应用程序,这很不方便。

Here is a snippet of my code: 这是我的代码片段:

   public class ThreadStarter
{
    public static void BuildScreen()
    {
        try
        {
            GlobalVariables.screenDrawer.onStart();
            GlobalVariables.listInitaliser.onStart();
            Logger.log("ThreadStarter.BuildScreen", "Threads started");
        }
        catch(IllegalThreadStateException e)
        {
            GlobalVariables.screenDrawer.StopThread();
            GlobalVariables.listInitaliser.StopThread();
            Logger.log("ThreadStarter.BuildScreen", "Threads stopped");

            GlobalVariables.screenDrawer.onStart();
            GlobalVariables.listInitaliser.onStart();
        }
        catch(Exception e)
        {
            Logger.Error("Couldn't stop or start the threads!");
            Logger.Error("Exception () Message: " + e.getMessage());
        }
    }
}

The threads: 线程:

    public class ListInitialiser extends Thread
{
    private static ListInitialiser _thread;
    public synchronized void run()
    {
        GlobalVariables.CurrentActivity.UpdateLists();

    }

    public  void onStart()
    {
        _thread = new ListInitialiser();
        _thread.start();
    }

    public void StopThread()
    {
        if (_thread != null)
        {
            _thread.interrupt();
            _thread = null;
        }
    }

}

I won't insert the ScreenDrawer thread here, because it's pretty much the same, except it calls another function. 我不会在这里插入ScreenDrawer线程,因为它几乎相同,只是它调用了另一个函数。

And this is how every activity is created (of course the contentView differs in each file): 这就是创建每个活动的方式(当然,每个文件的contentView都不相同):

@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getWindow().getAttributes().windowAnimations = R.style.Fade;
        setContentView(R.layout.activity_fine_data_3);
        GlobalVariables.CurrentActivity = this;
        ThreadStarter.BuildScreen();
        Logger.log("INFORMATION", "Person3DataActivity (Information 3/5)");
    }

In the GlobalVariables section I have these variables: 在GlobalVariables部分中,我具有以下变量:

public static ScreenDrawer screenDrawer = new ScreenDrawer();
public static ListInitialiser listInitaliser = new ListInitialiser();

If anyone has a solution or and idea, please share it with me. 如果有人有解决方案或想法,请与我分享。 Thanks in advance. 提前致谢。


EDIT: Okay, so I took onof's (rather harsh but useful :)) advice, and refactored my code to use AsyncTask instead. 编辑:好的,所以我接受了onof的(相当苛刻但有用的:))建议,并将我的代码重构为使用AsyncTask。 It seems to be working pretty fine. 它似乎工作得很好。 I managed to implement it into my AbstractActivity class, which is the parent of every Activity I use, and now all I have to do is call BuildScreen() method in every onCreate method. 我设法将其实现到AbstractActivity类中,该类是我使用的每个Activity的父级,现在我要做的就是在每个onCreate方法中调用BuildScreen()方法。

Thanks for the replies everyone. 感谢大家的答复。

try to add this to your class where u declared Global Variables 尝试将其添加到您声明了全局变量的类中

private static ListInitialiser instance;
public static synchronized ListInitialiser getInstance() {
    if (instance == null)
        instance = new ListInitialiser();
    return instance;
}

Everytime you donot have to create new when ur taking static.I dont know but may be this can help 每次您在使用static时都不必创建新文件。我不知道,但这可能会有所帮助

You can't rely on static variables as everything that is static (non final) in Android can be cleared any time the system need memory. 您不能依赖静态变量,因为只要系统需要内存,Android中的所有静态(非最终)内容都可以清除。 So don't think static = storage. 因此,不要以为静态=存储。

You should instead instantiate the objects when you need them, like following: 相反,应在需要它们时实例化对象,如下所示:

public static ScreenDrawer getScreenDrawer() {
    return new ScreenDrawer();
}

public static ListInitialiser getListInitialiser () {
    return new ListInitialiser ();
}

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

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