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

请帮我。

4

3 回答 3

2

这是因为

android:id="@+id/link_to_login" 用于 TextView 并且在您的代码中您已为 Button 声明它...所以更改它..

希望你明白......这将帮助你......

于 2013-08-23T08:47:56.600 回答
1

在 xml 中,您已将其定义为 TextView,而在 onCreate 中,您将其分配为导致 classCastexception 的按钮。在 xml/code 中更改它,它应该可以工作

于 2013-08-23T08:47:48.327 回答
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);
于 2013-08-23T08:52:18.193 回答