【问题标题】:Android apps cant even run.Logcat show "android.widget.text view cannot be cast to android.widget.Button."Android 应用程序甚至无法运行。Logcat 显示“android.widget.text 视图无法转换为 android.widget.Button。”
【发布时间】:2014-12-08 14:08:38
【问题描述】:

我是这里的 android 应用程序的新手..我的应用程序无法运行事件,我只是不知道为什么..那里的 logcat 一直显示这个

android.widget.text 视图不能转换为 android.widget.Button

这是我在 mainactivity.java 中的代码

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);
        }
    });
}
}

登录.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>

注册.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>

所以所有人都在这里期待,任何人都可以帮助我吗??? 提前致谢

【问题讨论】:

  • 您不能将 TextView 转换为 Button。 registerScreen 或 loginScreen 实际上是一个 TextView(可能两者都有,没有 xml 无法分辨)。
  • 如果您的 xml 布局文件中没有 TextView,请检查 stackoverflow.com/questions/12465551/…
  • 等待..我在这里上传我的 xml
  • 刚刚在这里添加了我的 login.xml 和 register.xml..
  • 您引用了错误的 ID。例如,引用 btnLogin(一个 Button)而不是 link_to_login(它是一个 TextView)

标签: java android xml


【解决方案1】:

添加

Button registerScreen = (Button) findViewById(R.id.btnLogin);

Button loginScreen = (Button) 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名称..

【讨论】:

  • 先生,我像你说的那样添加代码..但还是一样..我的xml有问题吗??
  • 好的,只是尝试将按钮的 id 名称更改为其他名称...在 main.xml 中
  • 哇..是工作兄弟..非常感谢..将main.xml中的ID更改为btnRegister和btnLogin..所有工作..tq非常感谢您的帮助..
  • @ken 我很高兴我能帮上忙……请接受它作为答案,以便其他人也可以得到帮助……:-)
【解决方案2】:

您的局部变量loginScreen 的类型为Button,但您调用findViewById() 的ID 为TextView。如果您真的想在文本字段之前挂上按钮,请使用R.id.btnRegister

【讨论】:

  • 我把它添加到这个 >Button registerScreen = (Button) findViewById (R.id.btnRegister);而这个 >Button loginScreen = (Button) findViewById(R.id.btnLogin);
  • 您需要提供更多细节,而不仅仅是“它无法工作”。您是否看到异常,您没有收到回调吗?如果您提供更新的代码和 logcat 输出,这也很有帮助。
猜你喜欢
  • 2011-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-29
  • 1970-01-01
  • 2020-09-02
相关资源
最近更新 更多