ViewRoot$CalledFromWrongThreadException on android app - android

I have read some threads regarding this and I did already take steps to resolve it.
I am using a handler (so that I don't update the UI on a separate thread) and so far I can't understand why this is still happening.
public class MyApp extends Activity implements OnClickListener, Runnable {
private ViewSwitcher switcher;
private static final int REFRESH_SCREEN = 1;
private boolean isValid = false;
private ProgressDialog dialog;
private TextView errorMessage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(this);
TextView errorMessage = (TextView)findViewById(R.id.txtErrorMessage);
errorMessage.setVisibility(View.GONE);
switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);
}
public void onClick(View v)
{
isValid = false;
dialog = ProgressDialog.show(ConcentraApp.this, "", "Loading. Please wait...", true);
Thread thread = new Thread(this);
thread.start();
}
public void run() {
String username = ((TextView)findViewById(R.id.txtUsername)).getText().toString();
String password = ((TextView)findViewById(R.id.txtPassword)).getText().toString();
errorMessage = (TextView)findViewById(R.id.txtErrorMessage);
errorMessage.setVisibility(View.GONE);
/* ... contact web service and get response ..*/
try {
/* get result from web service */
isValid = Boolean.parseBoolean(result);
if(isValid)
{
handler.sendEmptyMessage(1);
}
else
{
handler.sendEmptyMessage(0);
}
} catch (Exception e) {
handler.sendEmptyMessage(2);
isValid = false;
}
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
if(msg.what == 1)
{
errorMessage.setVisibility(View.VISIBLE);
errorMessage.setText("Correct login");
switcher.showNext();
}
else if(msg.what == 0)
{
errorMessage.setVisibility(View.VISIBLE);
errorMessage.setText("Invalid login");
}
else
{
errorMessage.setVisibility(View.VISIBLE);
errorMessage.setText("Internet error");
}
dialog.dismiss();
}
};
}
I am very new to this so I wouldn't be surprised if I'm missing something obvious.
It works fine without the thread, but then the process dialog doesn't show.
Many thanks in advance

You cannot call this:
errorMessage.setVisibility(View.GONE);
From a background thread. You should do it via handler as well.

Related

How to convert a non static handler to a static handler

I am having great difficulty to convert a non static handler into a static handler.
I have tried various approaches but every time I end up in doing something messy.
In the provided code please correct me how to achieve this. I did tried one example and I had to change most of my variables and functions into static which were referenced from the handler. But still i got additional errors.
public class MainActivity extends Activity implements Runnable {
private TextView textView;
boolean connectionToTupleSpace=false;
private TupleSpace ts = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.myText);
textView.setText("it is testing");
findViewById(R.id.login_button).setOnClickListener(buttonLogin);
}
public final Button.OnClickListener buttonLogin = new Button.OnClickListener() {
#Override
public void onClick(View v) {
Thread thread = new Thread(MainActivity.this);
thread.start();
}
};
#Override
public void run() {
Looper.prepare();
try {
ts = new TupleSpace("192.168.1.100",2525,"Orientation");
connectionToTupleSpace = true;
}catch (TupleSpaceException e) {
connectionToTupleSpace = false;
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
private Looper myLooper;
#Override
public void handleMessage(Message msg) {
if(connectionToTupleSpace == true)
{
Toast.makeText(getBaseContext(), " Tuple Space Server is Connected", Toast.LENGTH_SHORT).show();
showTuples();
}
else
{
/*Toast.makeText(getBaseContext(), " No connection to Tuple Space Server", Toast.LENGTH_SHORT).show();*/
showDialog("Connection", "there is no connection");
}
myLooper = Looper.myLooper();
Looper.loop();
myLooper.quit();
}
};
public void showTuples()
{
Tuple template = new Tuple(String.class, Integer.class);
Tuple[] returnTuple = null;
try {
returnTuple = ts.readAll(template);
} catch (TupleSpaceException e) {
e.printStackTrace();
}
int num = returnTuple.length;
if (num == 0)
System.out.print("No tuples in the space");
else
for(int i=0;i<num;i++)
{
System.out.print("\nTotal tuples are" + num+"\nYou found " + returnTuple[i]);
showDialog(returnTuple[i].getField(0).toString(),returnTuple[i].getField(1).toString());
}
}
private void showDialog(String title, String message)
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton("OK", null);
builder.show();
}
}
Just do like this
private Handler handler = new MyHandler(this);
private static class MyHandler extends Handler {
MainActivity activity;
public MyHandler(MainActivity activity) {
this.activity = activity;
}
#Override
public void handleMessage(Message msg) {
...
activity.showTuples();
...
}
}
To use your Activity inside static class (inside MyHandler), you must pass it like argument into the constructor.
EDIT: added constructor with MainActivity argument.

Progressdialog won't show up

I want to show this dialog, while the thread tries to build up a connection, but the dialog will not show up when I press the button which starts this method.
public void add_mpd(View view) {
dialog = ProgressDialog.show(MainActivity.this, "", "Trying to connect...");
new Thread(new Runnable() {
public void run() {
try {
String child;
EditText new_mpd = (EditText) findViewById(R.id.new_mpd);
child = new_mpd.getText().toString();
mpd = new MPD(child);
children.get(1).add(child);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MPDConnectionException e) {
e.printStackTrace();
}
}
}
).start();
adapter.notifyDataSetChanged();
dialog.dismiss();
}
It will not show up because the (blocking) work is done in another thread. That means, the start()-method of the Thread-class will not block.
Ergo, you show the Dialog, the Thread is started and the dialog is immediately dismissed (and therefore closed).
Put the call to dismiss() at the end of your run()-method and it should work just fine.
The above might be working for you, but you should not use the Thread-class directly. There are wrappers around it which are way more comfortable to use.
In Android, if you want to do long-term work off the UI-Thread, you should use an AsyncTask.
Additionaly, to build up on what Lukas said, you can look at this example.
http://www.helloandroid.com/tutorials/using-threads-and-progressdialog
public class ProgressDialogExample extends Activity implements Runnable {
private String pi_string;
private TextView tv;
private ProgressDialog pd;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.main);
tv.setText("Press any key to start calculation");
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
pd = ProgressDialog.show(this, "Working..", "Calculating Pi", true,
false);
Thread thread = new Thread(this);
thread.start();
return super.onKeyDown(keyCode, event);
}
public void run() {
pi_string = Pi.computePi(800).toString();
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
pd.dismiss();
tv.setText(pi_string);
}
};
}

Android Handler unable to access GUI elements

I feel kind of dumb asking this question because I have another app that this works for fine or at least I think it is the same. I definitely need another set of eyes on this because to me, it looks like it should work. My code is below:
public class InventorySystemActivity extends Activity {
private EditText barcode;
private EditText proddesc;
private EditText quantity;
public String scan_result;
public int which;
private InventorySystemDB db;
public Item singleItem;
public Item[] itemList;
private Thread t;
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
barcode.setText(scan_result);
proddesc.setText(singleItem.name);
quantity.setText(singleItem.quantity);
break;
case 3:
updateUI();
pd.dismiss();
}
}
};
public static Intent in = new Intent("com.google.zxing.client.android.SCAN");
private ProgressDialog pd;
private Thread dbt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.db = new InventorySystemDB(this);
try {
this.db.createDataBase();
this.db.openDataBase();
} catch (Exception e) {
e.printStackTrace();
}
this.dbt = new Thread(this.db);
setContentView(R.layout.main);
Button scan = (Button) findViewById(R.id.scan);
scan.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
startActivityForResult(in, 0);
}
});
Button search = (Button) findViewById(R.id.searchbutton);
search.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
pd.show();
which = 1;
scan_result = barcode.getText().toString();
dbt.start();
}
});
barcode = (EditText) findViewById(R.id.barcodenum);
proddesc = (EditText) findViewById(R.id.proddesc);
quantity = (EditText) findViewById(R.id.prodquantity);
pd = new ProgressDialog(this);
pd.setMessage("Loading data...");
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
scan_result = intent.getStringExtra("SCAN_RESULT");
barcode.setText(scan_result);
} else if (resultCode == RESULT_CANCELED) {
}
}
}
private void updateUI() {
barcode.setText(singleItem.upc);
proddesc.setText(singleItem.name);
quantity.setText("0");
}
}
All my thread does is it takes the UPC number and pulls up info from a couple websites. The code sends the message to the handler just fine and I've made a few apps like this using threads and handlers, not sure why I have this issue now. Anyway, I can dismiss the ProgressDialog, but I am unable to update any UI objects. It all looks fine to me, so I really need some more sets of eyes on this. Thanks guys.
You need to do UI changes in runOnUIThread()

Android Thread Exception?

i got thread exception in android , what i intend to do is, while clicking a button i started a thread going to dynamically invoke the handler ,handler update the text view with integer value , while reaching integer 10, i going to stop the thread and have to show an alert ,but it will cause an error, what i possibly doing is shown below
public class sample extends Activity implements Runnable{
public Camcorder()
{
try{
counterThread = new Thread(this);
}catch(Exception ee)
{
}
}
public void run()
{
try{
while(counterFlag)
{
System.out.println("The time starts at : "+counter);
Thread.sleep(1000);
calculate(counter);
counter++;
}
}catch(Exception ee){
System.out.println("Err in ee : "+ee);
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
c=this.getApplicationContext();
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
authalert3 = new AlertDialog.Builder(this);
authalert3.setTitle("Save Video");
authalert3.setMessage("Do you want to save this Video?");
authalert3.setPositiveButton("Yes", null);
Button test = (Button) findViewById(R.id.widget33);
test.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
counter = 0;
counterFlag = true;
counterThread.start();
}
});
public void calculate(int counter2) {
// TODO Auto-generated method stub
if(counter2<60){
if(counter2<10)
{
smin="0"+counter2;
}
else{
smin=""+counter2;
}
}
else{
hours++;
counter=0;
smin="00";
if(hours<10){
shours="0"+hours;
}
else{
shours=""+hours;
}
}
handler.sendEmptyMessage(0);
}
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
String tes=shours+":"+smin;
time.setText(tes);
test();
};
};
public void test(){
duration=1;
if(duration==hours){
counterFlag = false;
videoPath=camcorderView.stopRecording();
authalert3.create().show();
counterThread.stop();
}
}
the error is thrown at counterThread.stop();
Anyone suggest me , how to solve this error.
You don't stop threads by calling counterThread.stop. This method is deprecated. In your case, by setting counterFlag = false; your thread should be stopping itself.
You will also be getting an exception if you click twice on your button: you cannot call start on a Thread that has already been started. You must create a new instance of that Thread and start that new instance (stop the old instance before if necessary).
You can see that SO answer for some sample code on how to create/stop threads: Android thread in service issue. I suggest that you also read some tutorial on Java Threads (this is not specific to Android).
Additionally I think that you don't need a thread at all, you are doing nothing complicated and thus you could simply use the handler to do all the work:
private static final int MSG_REFRESH_UI = 0;
private static final int MSG_UPDATE_COUNTER = 1;
private int counter = 0;
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
if (msg.what==MSG_REFRESH_UI) {
String tes=shours+":"+smin;
time.setText(tes);
test();
} else if (msg.what==MSG_UPDATE_COUNTER) {
counter++;
if (counter<10) {
calculate(counter);
handler.sendEmptyMessageDelayed(MSG_UPDATE_COUNTER, 1000);
handler.sendEmptyMessage(MSG_REFRESH_UI);
}
}
};
};
public void onResume() {
handler.sendEmptyMessage(MSG_UPDATE_COUNTER);
}
public void calculate(int counter2) {
if (counter2<10) {
smin = "0"+counter2;
} else if (counter2<60) {
smin = ""+counter2;
} else{
hours++;
counter=0;
smin="00";
if(hours<10){
shours="0"+hours;
} else {
shours=""+hours;
}
}
}
This will stop the thread at 10
while(counterFlag)
{
System.out.println("The time starts at : "+counter);
Thread.sleep(1000);
calculate(counter);
counter++;
if(counter == 10) counterFlag = false;
}

Update UI from an event with a thread

Im working on a small application to try out an idea that I have. The idea is to periodically update the UI when event of some sort occurs. In the demo I've created, I'm updating a ProgressDialog every 2 seconds for 15 turns.
The problem I am having, which I don't quite understand is that when an event is handled, I send a message to the handler which is supposed to update the message in the ProgressDialog. When this happens however, I get an exception which states that I can't update the UI from that thread.
The following code appears in my Activity:
ProgressDialog diag;
String diagMessage = "Started loading...";
final static int MESSAGE_DATA_RECEIVED = 0;
final static int MESSAGE_RECEIVE_COMPLETED = 1;
final Handler handler = new Handler(){
#Override
public void handleMessage(Message msg){
diag.setMessage(diagMessage);
switch(msg.what){
case MESSAGE_DATA_RECEIVED:
break;
case MESSAGE_RECEIVE_COMPLETED:
dismissDialog();
killDialog();
break;
}
}
};
Boolean isRunning = false;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupDialog();
if(isRunning){
showDialog();
}
setContentView(R.layout.main);
}
void setupDialog(){
if(diag == null){
diag = new ProgressDialog(ThreadLoading.this);
diag.setMessage(diagMessage);
}
}
void showDialog(){
isRunning = true;
if(diag != null && !diag.isShowing()){
diag.show();
}
}
void dismissDialog(){
if(diag != null && diag.isShowing()){
diag.dismiss();
}
}
void killDialog(){
isRunning = false;
}
public void onStart(){
super.onStart();
showDialog();
Thread background = new Thread(new Runnable(){
public void run(){
try{
final ThreadRunner tr = new ThreadRunner();
tr.setOnDataReceivedListener(new ThreadRunner.OnDataReceivedListener(){
public void onDataReceived(String message){
diagMessage = message;
handler.handleMessage(handler.obtainMessage(MESSAGE_DATA_RECEIVED));
}
});
tr.setOnDataDownloadCompletedEventListener(new ThreadRunner.OnDataDownloadCompletedListener(){
public void onDataDownloadCompleted(String message){
diagMessage = message;
handler.handleMessage(handler.obtainMessage(MESSAGE_RECEIVE_COMPLETED));
}
});
tr.runProcess();
}
catch(Throwable t){
throw new RuntimeException(t);
}
}
});
background.start();
}
#Override
public void onPause(){
super.onPause();
dismissDialog();
}
For curiosity sake, here's the code for the ThreadRunner class:
public interface OnDataReceivedListener {
public void onDataReceived(String message);
}
public interface OnDataDownloadCompletedListener {
public void onDataDownloadCompleted(String message);
}
private OnDataReceivedListener onDataReceivedEventListener;
private OnDataDownloadCompletedListener onDataDownloadCompletedEventListener;
int maxLoop = 15;
int loopCount = 0;
int sleepTime = 2000;
public void setOnDataReceivedListener(OnDataReceivedListener onDataReceivedListener){
this.onDataReceivedEventListener = onDataReceivedListener;
}
public void setOnDataDownloadCompletedEventListener(OnDataDownloadCompletedListener onDataDownloadCompletedListener){
this.onDataDownloadCompletedEventListener = onDataDownloadCompletedListener;
}
public void runProcess(){
for(loopCount = 0; loopCount < maxLoop; loopCount++){
try{
Thread.sleep(sleepTime);
onDataReceivedEventListener.onDataReceived(Integer.toString(loopCount));
}
catch(Throwable t){
throw new RuntimeException(t);
}
}
onDataDownloadCompletedEventListener.onDataDownloadCompleted("Download is completed");
}
Am I missing something? The logic makes sense to me and it looks like everything should work, I'm using a handler to update the UI like it is recommended.
Any help will be appreciated.
Thanks,
Tyrone
P.S. I'm developing for Android 1.5
I found the problem. After comparing my code with someone else's code which was very similar, the following small problem was found:
handler.handleMessage(handler.obtainMessage(MESSAGE_RECEIVE_COMPLETED));
Should actually be:
handler.sendMessage(handler.obtainMessage(MESSAGE_RECEIVE_COMPLETED));
Hopefully someone finds this useful and learns from my mistake :)
Regards,
Tyrone

Categories

Resources