2013-08-23 122 views
1

在单击Loginform的textview时,我正在向我的注册表单重新显示页面。应用程序意外停止,请再试一次错误

在写入注册表格中的代码之前它工作正常。但突然间(在注册表单中写入代码之后),在登录形式中单击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

线28 Register.java请 – Raghunandan

回答

2

这是因为

机器人:ID =“@ + ID/link_to_login”是TextView的,在你的代码,你已经宣布它的按钮...所以变化它..

希望你能理解......这将帮助你....

+0

感谢SHANI,这是确切的问题 –

+1

这是正确答案。 –

1

在xml中,您已将其定义为TextView,并且在onCreate中将其指定为导致classCastexception的按钮。改变它在XML /代码,它应该工作

1

你有这样的

<TextView // text view with id link_to_login 
     android:id="@+id/link_to_login" 

当初始化你有这样的

Button btnRegister =(Button)findViewById(R.id.link_to_login); 

因此你类转换异常

要么改变你的TextView到按钮

<Button 
    android:id="@+id/link_to_login" 

TextView btnRegister =(TextView)findViewById(R.id.link_to_login); 
+0

Thanx的解决方案 –

+0

@NavatKayAahe你也可以声明那些最终editexts作为一个类的成员,并初始化它创建如果你不想要最终修改器 – Raghunandan

+0

它会影响代码? (意味着这些决赛是为了什么而写的?) –

相关问题