Java Android应用程序甚至无法运行。Logcat show“Android.widget.text视图无法转换为Android.widget.Button。”
我是android应用程序的新手,我的应用程序不能运行事件,我不知道为什么,那里的日志一直显示这个 无法将android.widget.text视图转换为android.widget.Button 这是我在mainactivity.java中的代码Java Android应用程序甚至无法运行。Logcat show“Android.widget.text视图无法转换为Android.widget.Button。”,java,android,xml,Java,Android,Xml,我是android应用程序的新手,我的应用程序不能运行事件,我不知道为什么,那里的日志一直显示这个 无法将android.widget.text视图转换为android.widget.Button 这是我在mainactivity.java中的代码 package com.loginandregister; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import a
package com.loginandregister;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
// setting default screen to login.xml
setContentView(R.layout.main);
Button registerScreen = (Button) findViewById(R.id.link_to_register);
// Listening to register new account link
registerScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(i);
}
});
Button loginScreen = (Button) findViewById (R.id.link_to_login);
// Listening to register new account link
loginScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(i);
}
});
}
}
login.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<!-- Header Starts-->
<LinearLayout
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@layout/header_gradient"
android:orientation="vertical"
android:paddingBottom="5dip"
android:paddingTop="5dip" >
<!-- Logo Start-->
<ImageView android:src="@drawable/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"/>
<!-- Logo Ends -->
</LinearLayout>
<!-- Header Ends -->
<!-- Footer Start -->
<LinearLayout android:orientation="horizontal"
android:id="@+id/footer"
android:layout_width="fill_parent"
android:layout_height="90dip"
android:background="@layout/footer_repeat"
android:layout_alignParentBottom="true">
</LinearLayout>
<!-- Footer Ends -->
<!-- Login Form -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_below="@id/header">
<!-- Email Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="Email"/>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginBottom="20dip"
android:singleLine="true" />
<!-- Password Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="Password"/>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:singleLine="true"
android:password="true"/>
<!-- Login button -->
<Button android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Login"/>
<!-- Link to Registration Screen -->
<TextView
android:id="@+id/link_to_register"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dip"
android:layout_marginTop="40dip"
android:gravity="center"
android:text="New to Plot Out?Register here"
android:textColor="#0b84aa"
android:textSize="20sp" />
</LinearLayout>
<!-- Login Form Ends -->
</RelativeLayout>
</ScrollView>
register.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#fff">
<!-- Header Starts-->
<LinearLayout android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@layout/header_gradient"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<!-- Logo Start-->
<ImageView android:src="@drawable/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"/>
<!-- Logo Ends -->
</LinearLayout>
<!-- Header Ends -->
<!-- Footer Start -->
<LinearLayout android:id="@+id/footer"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="90dip"
android:background="@layout/footer_repeat"
android:layout_alignParentBottom="true">
</LinearLayout>
<!-- Footer Ends -->
<!-- Registration Form -->
<!-- Registration Form Ends -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Full Name"
android:textColor="#372c24" />
<EditText
android:id="@+id/reg_fullname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="5dip"
android:singleLine="true" >
<requestFocus />
</EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Email"
android:textColor="#372c24" />
<EditText
android:id="@+id/reg_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="5dip"
android:singleLine="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Password"
android:textColor="#372c24" />
<EditText
android:id="@+id/reg_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:password="true"
android:singleLine="true" />
<Button
android:id="@+id/btnRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Register New Account" />
<TextView
android:id="@+id/link_to_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dip"
android:layout_marginTop="40dip"
android:gravity="center"
android:text="Already has account! Login here"
android:textColor="#ffffff"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/images" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignTop="@+id/textView1"
android:layout_marginTop="34dp"
android:text="Bring Me Out"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="56dp"
android:text="Plot Out"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="134dp"
android:text="Forgot Your Password?"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/link_to_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/link_to_register"
android:layout_centerHorizontal="true"
android:text="Login" />
<Button
android:id="@+id/link_to_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/link_to_login"
android:text="Register" />
>
</RelativeLayout>
所以大家都希望有人能在这方面帮助我???
提前感谢添加
Button registerScreen = (Button) findViewById(R.id.btnLogin);
按钮登录屏幕=按钮findViewById R.id.btnRegister
而不是
Button registerScreen = (Button) findViewById(R.id.link_to_register);
`Button loginScreen = (Button) findViewById (R.id.link_to_login);`
您正在强制转换文本视图的ID,而不是按钮。
或
更改main.xml中按钮的ID名称。您的局部变量loginScreen的类型为Button,但您正在使用TextView的ID调用findViewById。如果确实要在文本字段之前挂起按钮,请使用R.id.btnRegister。不能将文本视图强制转换为按钮。registerScreen或LoginsScreen实际上是一个文本视图,可能两者都是,没有xml就说不出来。如果xml布局文件中没有文本视图,请选中“等待”。我在此处上载我的xml只是在此处添加了我的login.xml和register.xml。您引用了错误的id。例如,在按钮中引用btnLogin,而不是链接到作为文本视图的登录。先生,我像你说的那样添加代码..但还是一样..我的xml中有什么错误吗?好的,试着把按钮的id名称改成其他的…在main.xmlwow..是工作的兄弟..非常感谢..在将main.xml中的id改成btnRegister和btnLogin之后..一切正常..非常感谢你的帮助..@ken我很高兴我能帮上忙不问题请接受它作为答案,以便其他人也能得到帮助…:-我将其添加到这个>按钮注册表screen=按钮findViewById R.id.btnRegister;这个>按钮登录屏幕=按钮findViewByIdR.id.btnLogin;你需要提供更多的细节,而不仅仅是它不能工作。您是否看到异常,是否未收到回调?如果您提供更新的代码和logcat输出,这也很有帮助。