Hi, You cannot make UI calls from outside the UI thread. So if you create a thread, you CANNOT modify the GUI without crashing. What you do is declare your handler in your onCreate function, and pass it to the thread. Then have the thread post a message (or a Runnable) to the handler, your code will work then.
On Thu, Jul 1, 2010 at 5:39 AM, Joel <[email protected]> wrote: > Greetings all, > > I have searched high & low and have not come up with the answer to > this, so please forgive me if I’ve missed something – I’m cross-eyed > at this point. > > I’m new to Android, but doing pretty well so far. I’m having a > problem displaying an AlertDialog inside of a thread. No matter what > I try, it crashes. If I use this code in the "onCreate" sub, it > works. The problem code is inside the “public void run()” section. > If I take out all the code relating to the AlertDialog, the error goes > away. I understand that I’m supposed to create the dialog using the > context of the Activity, and I thought my code does that – but > obviously I’ve missed something. If someone could explain to me what > I’m doing wrong, I would be eternally grateful. > > I’ve had to remove a few items from the code for privacy reasons, they > mention my company name and the product I’m developing, etc etc. > > Here’s the code: > > ------------------------------------------------------------------------------------------------------------------ > > package com.[removed for privacy].[removed for privacy]; > > import java.io.BufferedReader; > import java.io.IOException; > import java.io.InputStream; > import java.io.InputStreamReader; > import java.net.URI; > import java.net.URISyntaxException; > > import org.apache.http.HttpResponse; > import org.apache.http.client.ClientProtocolException; > import org.apache.http.client.methods.HttpGet; > import org.apache.http.impl.client.DefaultHttpClient; > > import android.app.Activity; > import android.app.AlertDialog; > import android.app.ProgressDialog; > import android.content.Context; > import android.content.DialogInterface; > import android.os.Bundle; > import android.os.Handler; > import android.os.Message; > import android.telephony.TelephonyManager; > import android.view.Menu; > import android.view.MenuItem; > > public class Init extends Activity implements Runnable { > > public String DROIDPHONENUMBER; > public String HTTP_RESULT; > public int MINUTESLEFT; > public ProgressDialog pd; > > private String getMyPhoneNumber() { > TelephonyManager mTelephonyMgr = > (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); > return mTelephonyMgr.getLine1Number(); > } > > /** Called when the activity is first created. */ > �...@override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.init); > DROIDPHONENUMBER = getMyPhoneNumber(); > pd = ProgressDialog.show(this, "Test Title", "Test Message…", > true, false); > Thread getBalanceThread = new Thread(this); > getBalanceThread.start(); > } > > public String getUrlData(String url) { > String websiteData = null; > try { > DefaultHttpClient client = new DefaultHttpClient(); > URI uri = new URI(url); > HttpGet method = new HttpGet(uri); > HttpResponse res = client.execute(method); > InputStream data = res.getEntity().getContent(); > websiteData = generateString(data); > } > catch (ClientProtocolException e) { > e.printStackTrace(); > } > catch (IOException e) { > e.printStackTrace(); > } > catch (URISyntaxException e) { > e.printStackTrace(); > } > return websiteData; > } > > public String generateString(InputStream stream) { > InputStreamReader reader = new InputStreamReader(stream); > BufferedReader buffer = new BufferedReader(reader); > StringBuilder sb = new StringBuilder(); > try { > String cur; > while ((cur = buffer.readLine()) != null) { > sb.append(cur); > } > } > catch (IOException e) { > e.printStackTrace(); > } > try { > stream.close(); > } catch (IOException e) { > e.printStackTrace(); > } > return sb.toString(); > } > > /** MENU FUNCTIONS **/ > �...@override > public boolean onCreateOptionsMenu(Menu menu) { > menu.add("Close"); > return true; > } > > public boolean onOptionsItemSelected(MenuItem item) { > if (item.getTitle() == "Close") { > finish(); > } > return true; > } > > public void run() { > HTTP_RESULT = getUrlData("https://[url removed for > privacy]" + DROIDPHONENUMBER); > AlertDialog.Builder builder = new > AlertDialog.Builder(Init.this); > builder.setTitle("Test Title"); > builder.setMessage(HTTP_RESULT); > builder.setPositiveButton("OK", new > DialogInterface.OnClickListener() { > public void onClick(DialogInterface dialog, int which) { > return; > } > }); > AlertDialog dialog = builder.create(); > dialog.setOwnerActivity(Init.this); > dialog.show(); > > handler.sendEmptyMessage(0); > } > > private Handler handler = new Handler() { > public void handleMessage(Message msg) { > pd.dismiss(); > } > }; > } > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en -- http://developingthedream.blogspot.com/, http://diastrofunk.com, http://www.youtube.com/user/revoltingx, ~Isaiah 55:8-9 -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

