Android新出现的java异常无法强制转换为Android。小装置。编辑文本
我是安卓新手,在一个基本应用程序上遇到了一个错误。我得到了以下错误
Caused by: java.lang.ClassCastException: 安卓.widget.RelativeLayout cannot be cast to 安卓.widget.EditText at com.example.chris.mytestv1.MainActivity.onCreate(MainActivity.java:39)
下面是导致它的原因
numberTxt = (EditText) findViewById(R.id.numberTxt);
在我的主要活动中。我有
package com.example.chris.mytestv1;
import 安卓.net.Uri;
import 安卓.os.Bundle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.support.v7.widget.Toolbar;
import 安卓.view.View;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.TextView;
import com.google.安卓.gms.appindexing.Action;
import com.google.安卓.gms.appindexing.AppIndex;
import com.google.安卓.gms.common.api.GoogleApiClient;
public class MainActivity extends AppCompatActivity {
TextView totalTextView = null;
EditText percentageTxt = null;
EditText numberTxt = null;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
totalTextView = (TextView) findViewById(R.id.TotalTextView);
percentageTxt = (EditText) findViewById(R.id.percentageTxt);
numberTxt = (EditText) findViewById(R.id.numberTxt);
Button calcBtn = (Button) findViewById(R.id.calcBtn);
calcBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float percentage = Float.parseFloat(percentageTxt.getText().toString());
float dec = percentage / 100;
float total = dec * Float.parseFloat(numberTxt.getText().toString());
totalTextView.setText(Float.toString(total));
}
});
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("安卓-app://com.example.chris.mytestv1/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("安卓-app://com.example.chris.mytestv1/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}
在我的主要内容中。我有-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.chris.mytestv1.MainActivity"
tools:showIn="@layout/activity_main"
安卓:id="@+id/numberTxt">
<TextView
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:text="0"
安卓:layout_alignParentTop="true"
安卓:layout_centerHorizontal="true"
安卓:layout_marginTop="41dp"
安卓:textAlignment="center"
安卓:textSize="50dp"
安卓:id="@+id/TotalTextView" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:text="What is"
安卓:id="@+id/textView"
安卓:layout_below="@+id/TotalTextView"
安卓:layout_centerHorizontal="true"
安卓:layout_marginTop="41dp"
安卓:layout_marginBottom="10dp" />
<EditText
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:inputType="numberDecimal"
安卓:ems="10"
安卓:id="@+id/percentageTxt"
安卓:layout_below="@+id/textView"
安卓:layout_centerHorizontal="true"
安卓:hint="Enter Percentage"
安卓:textAlignment="center" />
<EditText
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:inputType="numberDecimal"
安卓:ems="10"
安卓:id="@+id/numberTxt"
安卓:hint="Enter Number"
安卓:textAlignment="center"
安卓:layout_marginTop="48dp"
安卓:layout_below="@+id/textView3"
安卓:layout_alignStart="@+id/percentageTxt" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:text="%"
安卓:id="@+id/textView2"
安卓:layout_marginRight="50dp"
安卓:layout_alignTop="@+id/percentageTxt"
安卓:layout_toEndOf="@+id/calcBtn"
安卓:paddingTop="10dp" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:text="Of"
安卓:id="@+id/textView3"
安卓:layout_centerVertical="true"
安卓:layout_centerHorizontal="true" />
<Button
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="CALCULATE"
安卓:id="@+id/calcBtn"
安卓:layout_marginTop="70dp"
安卓:textSize="30dp"
安卓:background="#b91313"
安卓:textColor="#ffffff"
安卓:layout_below="@+id/numberTxt"
安卓:layout_centerHorizontal="true" />
</RelativeLayout>
我一直在寻找为什么会发生这种错误,但我无法找到原因
# 1 楼答案
id重复,RelativeLayout和EditText使用相同的id:
所以,当安卓系统试图找到可视文本时,他会首先尝试使用它,而不是EditText
# 2 楼答案
我发现了错误,愚蠢的错误。我不确定该去哪里找