Sunday, 11 August 2013

Why does my application crash?

Why does my application crash?

I am new to android, and am learning about tasks. I found this example in
a tutorial on using runnable.
public class ThreadExample extends Activity {
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
TextView myTextView = (TextView)findViewById(R.id.myTextView);
String ReturnedInt = Integer.toString(msg.what);
myTextView.setText(ReturnedInt);
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1500); // Vibrate for 1500 milliseconds
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thread_example);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.thread_example, menu);
return true;
}
public void buttonClick(View view)
{
Runnable runnable = new Runnable() {
public void run() {
long endTime = System.currentTimeMillis() + 2*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
}
catch (Exception e) {
}
}
}
handler.sendEmptyMessage(1234);
}
};
Thread mythread = new Thread(runnable);
mythread.start();
}
}
This code works. If I press the button on the screen, the text box reads
"1234" and the vibrator vibrates. I wanted to rearrange the code a bit, to
meet my ultimate goal of writing an audio recorder, where the audio
sampling would be in the thread. I rearranged the code by moving the
runnable and thread declarations to the top of the class, so I could
reference them from other methods
public class ThreadExample extends Activity {
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
TextView myTextView = (TextView)findViewById(R.id.myTextView);
String ReturnedInt = Integer.toString(msg.what);
myTextView.setText(ReturnedInt);
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1500); // Vibrate for 1500 milliseconds
}
};
Runnable runnable = new Runnable() {
public void run() {
long endTime = System.currentTimeMillis() + 2*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
}
catch (Exception e) {
}
}
}
handler.sendEmptyMessage(1234);
}
};
Thread mythread = new Thread(runnable);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thread_example);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.thread_example, menu);
return true;
}
public void buttonClick(View view)
{
mythread.start();
}
}
If a run the application again, is works the first time i press the
button, the text is updated and the vibrator vibrates, but the second time
I press the button the program crashes. I think it may be something to do
with the thread being destroyed, but I thought the declarations at the
start of the class would not be destroyed until the class was destroyed.
It may be something else.
Any Ideas why it crashes the second time I hit the button?
Edit - As requested, here is the logcat output
08-11 09:49:37.489: W/dalvikvm(5014): threadid=1: thread exiting with
uncaught exception (group=0x2aac8560) 08-11 09:49:37.509:
E/AndroidRuntime(5014): FATAL EXCEPTION: main 08-11 09:49:37.509:
E/AndroidRuntime(5014): java.lang.IllegalStateException: Could not execute
method of the activity 08-11 09:49:37.509: E/AndroidRuntime(5014): at
android.view.View$1.onClick(View.java:2168) 08-11 09:49:37.509:
E/AndroidRuntime(5014): at android.view.View.performClick(View.java:2552)
08-11 09:49:37.509: E/AndroidRuntime(5014): at
android.view.View$PerformClick.run(View.java:9229) 08-11 09:49:37.509:
E/AndroidRuntime(5014): at
android.os.Handler.handleCallback(Handler.java:587) 08-11 09:49:37.509:
E/AndroidRuntime(5014): at
android.os.Handler.dispatchMessage(Handler.java:92) 08-11 09:49:37.509:
E/AndroidRuntime(5014): at android.os.Looper.loop(Looper.java:123) 08-11
09:49:37.509: E/AndroidRuntime(5014): at
android.app.ActivityThread.main(ActivityThread.java:3701) 08-11
09:49:37.509: E/AndroidRuntime(5014): at
java.lang.reflect.Method.invokeNative(Native Method) 08-11 09:49:37.509:
E/AndroidRuntime(5014): at
java.lang.reflect.Method.invoke(Method.java:507) 08-11 09:49:37.509:
E/AndroidRuntime(5014): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
08-11 09:49:37.509: E/AndroidRuntime(5014): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624) 08-11
09:49:37.509: E/AndroidRuntime(5014): at
dalvik.system.NativeStart.main(Native Method) 08-11 09:49:37.509:
E/AndroidRuntime(5014): Caused by:
java.lang.reflect.InvocationTargetException 08-11 09:49:37.509:
E/AndroidRuntime(5014): at java.lang.reflect.Method.invokeNative(Native
Method) 08-11 09:49:37.509: E/AndroidRuntime(5014): at
java.lang.reflect.Method.invoke(Method.java:507) 08-11 09:49:37.509:
E/AndroidRuntime(5014): at android.view.View$1.onClick(View.java:2163)
08-11 09:49:37.509: E/AndroidRuntime(5014): ... 11 more 08-11
09:49:37.509: E/AndroidRuntime(5014): Caused by:
java.lang.IllegalThreadStateException: Thread already started. 08-11
09:49:37.509: E/AndroidRuntime(5014): at
java.lang.Thread.start(Thread.java:1227) 08-11 09:49:37.509:
E/AndroidRuntime(5014): at
com.example.threadexample.ThreadExample.buttonClick(ThreadExample.java:60)
08-11 09:49:37.509: E/AndroidRuntime(5014): ... 14 more

No comments:

Post a Comment