点击Loginform的textview后,我正在将页面翻到我的注册表单。
在注册表格中编写代码之前,它工作正常。但是突然(在注册表单中编写代码之后),在loginform中单击textview(“请注册自己”)后,它开始给我上述错误。
查看logcat后,我发现注册页面上有错误。
以下是我的注册页面上的代码:
public class Register extends Activity implements OnClickListener,OnCheckedChangeListener {
String gender=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//Toast.makeText(getApplicationContext(),"Load" , Toast.LENGTH_LONG).show();
Button btnRegister =(Button)findViewById(R.id.link_to_login);
final EditText edFullName=(EditText)findViewById(R.id.txtfname);
final EditText edUserName=(EditText)findViewById(R.id.txtusername);
final EditText edEmail=(EditText)findViewById(R.id.txtemail);
final RadioButton rbMale=(RadioButton)findViewById(R.id.rd_male);
final RadioButton rbFeMale=(RadioButton)findViewById(R.id.rd_female);
final EditText edDateOfBirth=(EditText)findViewById(R.id.txtdob);
final EditText edPassward=(EditText)findViewById(R.id.txtpassword);
rbFeMale.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(arg1==true)
{
gender="Female";
}
}
});
rbMale.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(arg1==true)
{
gender="Male";
}
}
});
final AlertDialog ad=new AlertDialog.Builder(this).create();
btnRegister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CallSoap cs=new CallSoap();
try
{
Boolean result=cs.Register(edFullName.getText().toString(), edUserName.getText().toString(), edEmail.getText().toString(), gender, edDateOfBirth.getText().toString(), edPassward.getText().toString());
if (result==true)
{
Toast.makeText(getApplicationContext(),"Registered Sucessfully!!!" , Toast.LENGTH_LONG).show();
}
}
catch(Exception ex)
{
ad.setMessage(ex.getMessage());
}
}
});
TextView tvBackLogin=(TextView)findViewById(R.id.link_to_login);
tvBackLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Register.this,MainActivity.class);
//intent.putExtra(EXTRA_MESSAGE, etLoginID.getText().toString());
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.register, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
}
}
这是我的registration.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Full Name" />
<EditText
android:id="@+id/txtfname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/tvRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Username" />
<EditText
android:id="@+id/txtusername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Email ID" />
<EditText
android:id="@+id/txtemail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Gender" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/rd_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Male" />
<RadioButton
android:id="@+id/rd_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<TextView
android:id="@+id/textView4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Date of Birth" />
<EditText
android:id="@+id/txtdob"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date" />
<TextView
android:id="@+id/textView5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Password" />
<EditText
android:id="@+id/txtpassword"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/btnRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Register" />
<TextView
android:id="@+id/link_to_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="Already have account! Login here" />
</LinearLayout>
</ScrollView>
堆栈跟踪:
08-23 13:12:11.154: E/AndroidRuntime(9864): FATAL EXCEPTION: main
08-23 13:12:11.154: E/AndroidRuntime(9864): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androsqlapp/com.example.androsqlapp.Register}: java.lang.ClassCastException: android.widget.TextView
08-23 13:12:11.154: E/AndroidRuntime(9864): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-23 13:12:11.154: E/AndroidRuntime(9864): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-23 13:12:11.154: E/AndroidRuntime(9864): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-23 13:12:11.154: E/AndroidRuntime(9864): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-23 13:12:11.154: E/AndroidRuntime(9864): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 13:12:11.154: E/AndroidRuntime(9864): at android.os.Looper.loop(Looper.java:123)
08-23 13:12:11.154: E/AndroidRuntime(9864): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-23 13:12:11.154: E/AndroidRuntime(9864): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 13:12:11.154: E/AndroidRuntime(9864): at java.lang.reflect.Method.invoke(Method.java:507)
08-23 13:12:11.154: E/AndroidRuntime(9864): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-23 13:12:11.154: E/AndroidRuntime(9864): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-23 13:12:11.154: E/AndroidRuntime(9864): at dalvik.system.NativeStart.main(Native Method)
08-23 13:12:11.154: E/AndroidRuntime(9864): Caused by: java.lang.ClassCastException: android.widget.TextView
08-23 13:12:11.154: E/AndroidRuntime(9864): at com.example.androsqlapp.Register.onCreate(Register.java:28)
08-23 13:12:11.154: E/AndroidRuntime(9864): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-23 13:12:11.154: E/AndroidRuntime(9864): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-23 13:12:11.154: E/AndroidRuntime(9864): ... 11 more
请帮帮我。
答案 0 :(得分:2)
这是因为
android:id =“@ + id / link_to_login”用于TextView,在你的代码中你已经为Button声明了......所以改变它..
希望你明白......这会对你有帮助......
答案 1 :(得分:1)
在xml中,您已将其定义为TextView,而在onCreate中,您将其指定为导致classCastexception的按钮。在xml /代码中更改它应该可以工作
答案 2 :(得分:1)
你有这个
<TextView // text view with id link_to_login
android:id="@+id/link_to_login"
初始化时你有这个
Button btnRegister =(Button)findViewById(R.id.link_to_login);
因此你得到了类强制转换异常
将文本视图更改为按钮
<Button
android:id="@+id/link_to_login"
或
TextView btnRegister =(TextView)findViewById(R.id.link_to_login);