Creation of New Thread is Giving an error
I am new to android i am trying to create a new thread to invoke another method. But don't why it is throwing the error.
here is my stub
void test()
{
int i=0;
Toast.makeText(getApplicationContext(), "Testing", Toast.LENGTH_SHORT).show();
}
public void Button2_Click(View v)
{
Thread thread = new Thread()
{
@Override
public void run() {
开发者_开发知识库 test();
}
};
thread.start();
}
You can't manipulate the UI from other threads than the main thread, and launching a Toast involves the user interface.
change your test function
void test()
{
int i=0;
Log.d("Test","Testing");
}
now if the thread works you will se a log inside LogCat. If you want to display a Toast from another thread, you must use Handler or runOnUiThread.
Do this is Handler
like this http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html
you can not put Toast inside a Thread. Remove your Toast message from your code. It will work fine.
This is how you can do it.
`new Thread(new Runnable() {
public void run() {
Bitmap b = loadImageFromNetwork();
mImageView.setImageBitmap(b);
}
}).start();
'
Its a bracket mismatch.
If you absolutely must Toast, you should use the runOnUIThread() method and pass it a new Runnable() anonymous class that Toasts in the run() method.
Irrelevantly, this doesn't seem to make a whole lot of sense. Perhaps you want to make test() static. Maybe post a better idea of what you'd like to do and exactly what error you're getting?
精彩评论