我的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 文件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);
}
}
当我运行应用程序时,我得到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
据我了解,我没有将 Button 转换为编辑文本。我不确定,为什么我会收到这个错误。