在我的android编程代码java.lang中获取错误

时间:2017-06-26 17:03:22

标签: java android android-activity

这是我的java文件UserAreaActivity文件。检查此代码并以代码格式提供ans。 m使用最新版本的android studio。

package com.symplycode.newloginregister;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class UserAreaActivity extends AppCompatActivity {

    @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_area);

        final EditText etUsername=(EditText) findViewById(R.id.etUsername);
        final EditText etAge=(EditText) findViewById(R.id.etAge);
        final TextView welcomeMessage=(TextView) findViewById(R.id.tvWelcomeMsg);


        Bundle extras=getIntent().getExtras();
        String name= extras.getString("name");
        String username= extras.getString("username");
        String age= extras.getString("age");

        String message = name + "welcome to your user area";
        welcomeMessage.setText(message);
        etUsername.setText(username);
        etAge.setText(age);
    }
}

这是xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:padding="20dp"
    tools:context="com.symplycode.newloginregister.UserAreaActivity">

    <TextView
        android:id="@+id/tvWelcomeMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textSize="24sp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/tvWelcomeMsg"
        android:layout_marginTop="30dp"
        android:paddingLeft="6dp"
        android:text="Name" />

    <TextView
        android:id="@+id/etName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView10"
        android:ems="10"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/textView11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/etName"
        android:paddingLeft="6dp"
        android:text="Age" />

    <TextView
        android:id="@+id/etAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView11"
        android:ems="10"
        android:inputType="textPersonName" />
</RelativeLayout>

在这里得到错误..我怎么解决这个问题请告诉我

java.lang.RuntimeException:无法启动活动ComponentInfo {com.symplycode.newloginregister / com.symplycode.newloginregister.UserAreaActivity}:java.lang.ClassCastException:android.support.v7.widget.AppCompatTextView无法强制转换为android .widget.EditText

1 个答案:

答案 0 :(得分:1)

 final EditText etUsername=(EditText) findViewById(R.id.etUsername);
        final EditText etAge=(EditText) findViewById(R.id.etAge);

必须是

 final TextView etUsername=(TextView) findViewById(R.id.etName);
        final TextView etAge=(TextView) findViewById(R.id.etAge);

您正在尝试将TextView窗口小部件转换为EditText窗口小部件。要么像我的示例那样更改它,要么将xml布局中的TextView更改为EditText

顺便说一下,您的布局中没有ID为etUsername的视图,也许这是一个错字,您的意思是etName?确保使用正确的布局xml ...

您使用setContentView(R.layout.activity_user_area);设置的布局必须是两个视图都位于其中的布局...