Reputation: 3789
My Activity_main.xml
<Button
android:id="@+id/MyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="@string/button" />
<EditText
android:id="@+id/PasswordText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/PasswordText"
android:inputType="textPassword" />
<EditText
android:id="@+id/UsernameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="@string/UsernameText" >
<requestFocus />
</EditText>
Java File MainAcivity.java
package com.example.buttonclicktutorial;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener{
EditText name;
EditText pass;
Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.UsernameText);
pass = (EditText) findViewById(R.id.PasswordText);
login = (Button) findViewById(R.id.MyButton);
login.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(MainActivity.this,
NewActivity.class);
startActivity(myIntent);
}
}
When I am running the app I am getting ClassCastException
10-08 13:28:11.970: E/AndroidRuntime(6674): FATAL EXCEPTION: main
10-08 13:28:11.970: E/AndroidRuntime(6674): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttonclicktutorial/com.example.buttonclicktutorial.MainActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
10-08 13:28:11.970: E/AndroidRuntime(6674): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1978)
10-08 13:28:11.970: E/AndroidRuntime(6674): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2003)
10-08 13:28:11.970: E/AndroidRuntime(6674): at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-08 13:28:11.970: E/AndroidRuntime(6674): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1169)
10-08 13:28:11.970: E/AndroidRuntime(6674): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 13:28:11.970: E/AndroidRuntime(6674): at android.os.Looper.loop(Looper.java:137)
10-08 13:28:11.970: E/AndroidRuntime(6674): at android.app.ActivityThread.main(ActivityThread.java:4446)
10-08 13:28:11.970: E/AndroidRuntime(6674): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 13:28:11.970: E/AndroidRuntime(6674): at java.lang.reflect.Method.invoke(Method.java:511)
10-08 13:28:11.970: E/AndroidRuntime(6674): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-08 13:28:11.970: E/AndroidRuntime(6674): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-08 13:28:11.970: E/AndroidRuntime(6674): at dalvik.system.NativeStart.main(Native Method)
10-08 13:28:11.970: E/AndroidRuntime(6674): Caused by: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.EditText
10-08 13:28:11.970: E/AndroidRuntime(6674): at com.example.buttonclicktutorial.MainActivity.onCreate(MainActivity.java:20)
10-08 13:28:11.970: E/AndroidRuntime(6674): at android.app.Activity.performCreate(Activity.java:4465)
10-08 13:28:11.970: E/AndroidRuntime(6674): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-08 13:28:11.970: E/AndroidRuntime(6674): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1942)
10-08 13:28:11.970: E/AndroidRuntime(6674): ... 11 more
As far as I understand , the I am not casting Button as an edittext. I am not sure , why I am getting this error.
Upvotes: 0
Views: 959
Reputation: 43023
That's a funny one.
Have you tried cleaning the project and trying again? It often helps in such situations - the resource files (ID's) get out of order.
Upvotes: 3
Reputation: 2127
Just clean your project and rebuild it.
This often occurs after you edit or refactor some of your xml layout files.
The Auto generated resource ids are in a mess. Editor's id becomes Button's.
Upvotes: 1