开发者

Android - Starting a thread crashes app

开发者 https://www.devze.com 2023-03-15 19:12 出处:网络
I\'ve got a few activities.In the main activity I have a login screen, when the user presses the login button, a thread is started to show a p开发者_开发技巧rogress dialog until the user has been auth

I've got a few activities. In the main activity I have a login screen, when the user presses the login button, a thread is started to show a p开发者_开发技巧rogress dialog until the user has been authenticated. At this point i load the next activity which has several fields for the user to input data.

Here the user inputs some data and presses a button to process it. The data is passed to a new activity where the data is actually processed and displayed. This is where i create the new thread and where it's crashing when i call thread.start(); and I have no idea why this is happening.

Both activities are implementing Runnable.

I'm using the same code below to create and call thread.start() in the button press of the first activity and the onCreate method of the last one:

pd = ProgressDialog.show(search_results.this, "", "Searching...", true, false);
    Thread thread = new Thread(this);
    thread.start();

I'm using the same code below as well to handle the threads for both as well.

public void run() {
    handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {            
            pd.dismiss();               
    }
};

Am I missing something? I don't really understand why it's crashing.


While I encourage people to use AsyncTask, it's not really needed, especially for simple things like progress/loading dialogs. That's not the problem here.

Your question and your code is confusing. I'm not sure which code goes where, on which activity, and I hope you're not leaving dialogs open between activities, trying to access them across them (it won't work, of course). Also, providing a Context to a Thread does not even compile (it's marked with errors at design time). To sum it all up, you didn't provide the Log entry. Sorry, I can't make sense of what you're doing or where the error is. We can only guess.

Below are one of the possible ways to do it with a Handler, a Runnable and a Thread. This was taken from the Developer Resource when I first learn how to use it:

1- You declare a Handler. Most people do this on the onCreate section to reuse it often:

Handler mHandler = new Handler();

2- When you need, you start a Thread:

new Thread() { public void run() {
    mHandler.post(mLoadingData);
    // ... do work
    mHandler.post(mLoadingDataStop);
}}.start()

3- These are the Runnables that are posted to the Handler:

private final Runnable mLoadingData = new Runnable() {public void run() {
    showDialog(LOADING_DIALOG); // In your case, show your custom dialog
}};

private final Runnable mLoadingDataStop = new Runnable() {public void run() {
    dismissDialog(LOADING_DIALOG); // In your case, dismiss the dialog
}};

For a progress dialog, things need a bit more work (update the progress etc.), but for a loading dialog, you don't need to really mess with messages.


I had this same issue when developing for the tablet. After a certain API, I'm thinking 3.0 (sdk 11), Android enforces applications to run long running processes on a separate thread, otherwise it kills it. Logcat will confirm this.

I know you are using another thread, but that didn't work for me either. Try using AsyncTask. You can create a quick inner class that, in my opinion, is way easier than handling your own threads. AsyncTask has several functions that run on the UI thread and a couple that run on their own thread. This allows you to start a "Loading" user interface object on the user interface thread, process on the back end thread, and then when its done, it'll notify a user interface thread function.

You'll want to specifically look at override

onPreExecute() doInBackground() onPostExecute()

0

精彩评论

暂无评论...
验证码 换一张
取 消