Reputation: 25
I am new to android developement, while trying to create an editable TextView I ran into the following exception.
Here is the full stack trace of the exception
09-17 01:25:14.749: E/AndroidRuntime(8436): FATAL EXCEPTION: main
09-17 01:25:14.749: E/AndroidRuntime(8436): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.timer/com.example.timer.TimerHomeActivity}: java.lang.ClassCastException: android.widget.TextView
09-17 01:25:14.749: E/AndroidRuntime(8436): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
09-17 01:25:14.749: E/AndroidRuntime(8436): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
09-17 01:25:14.749: E/AndroidRuntime(8436): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-17 01:25:14.749: E/AndroidRuntime(8436): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-17 01:25:14.749: E/AndroidRuntime(8436): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 01:25:14.749: E/AndroidRuntime(8436): at android.os.Looper.loop(Looper.java:130)
09-17 01:25:14.749: E/AndroidRuntime(8436): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-17 01:25:14.749: E/AndroidRuntime(8436): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 01:25:14.749: E/AndroidRuntime(8436): at java.lang.reflect.Method.invoke(Method.java:507)
09-17 01:25:14.749: E/AndroidRuntime(8436): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-17 01:25:14.749: E/AndroidRuntime(8436): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-17 01:25:14.749: E/AndroidRuntime(8436): at dalvik.system.NativeStart.main(Native Method)
09-17 01:25:14.749: E/AndroidRuntime(8436): Caused by: java.lang.ClassCastException: android.widget.TextView
09-17 01:25:14.749: E/AndroidRuntime(8436): at com.example.timer.TimerHomeActivity.onCreate(TimerHomeActivity.java:23)
09-17 01:25:14.749: E/AndroidRuntime(8436): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-17 01:25:14.749: E/AndroidRuntime(8436): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
09-17 01:25:14.749: E/AndroidRuntime(8436): ... 11 more
This is the Code:
public class TimerHomeActivity extends Activity implements OnClickListener {
public void onClick(View v) {
TextView tv = (TextView)findViewById(R.id.timer_1);
tv.setText("Hi");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timer_home);
Button timer1 = (Button)findViewById(R.id.timer_1);
timer1.setOnClickListener(this);
}
}
Upvotes: 1
Views: 385
Reputation: 72844
Check your resource IDs. You are loading the same resource ID twice, once converting it to a TextView
and once to a Button
:
TextView tv = (TextView)findViewById(R.id.timer_1);
and
Button timer1 = (Button)findViewById(R.id.timer_1);
It should correspond to one widget and the other will throw a ClassCastException
.
Since the exception is occuring when starting the activity, the statement causing the exception should be the one in the onCreate
method. This means resource timer_1
is not a Button
but a TextView
(as indicated in the exception message which by the way seems to be truncated in your post).
Upvotes: 3