Ich bin neu bei Android und bin in einem Fehler auf einer grundlegenden App. Ich bin den folgenden Fehler bekommen,Neu bei Android - Ausnahme - kann nicht in android.widget.EditText umgewandelt werden
Verursacht durch: java.lang.ClassCastException: android.widget.RelativeLayout kann nicht auf android.widget.EditText bei com.example.chris.mytestv1.MainActivity.onCreate gegossen werden (MainActivity.java:39)
Im Folgenden wird die Linie es
numberTxt = (EditText) findViewById(R.id.numberTxt);
In meinem MainActivity.java verursacht habe ich
package com.example.chris.mytestv1;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.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("android-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("android-app://com.example.chris.mytestv1/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}
In meinem content_main.xml ich habe -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android: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"
android:id="@+id/numberTxt">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="0"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:textAlignment="center"
android:textSize="50dp"
android:id="@+id/TotalTextView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="What is"
android:id="@+id/textView"
android:layout_below="@+id/TotalTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:layout_marginBottom="10dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/percentageTxt"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:hint="Enter Percentage"
android:textAlignment="center" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/numberTxt"
android:hint="Enter Number"
android:textAlignment="center"
android:layout_marginTop="48dp"
android:layout_below="@+id/textView3"
android:layout_alignStart="@+id/percentageTxt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="%"
android:id="@+id/textView2"
android:layout_marginRight="50dp"
android:layout_alignTop="@+id/percentageTxt"
android:layout_toEndOf="@+id/calcBtn"
android:paddingTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Of"
android:id="@+id/textView3"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALCULATE"
android:id="@+id/calcBtn"
android:layout_marginTop="70dp"
android:textSize="30dp"
android:background="#b91313"
android:textColor="#ffffff"
android:layout_below="@+id/numberTxt"
android:layout_centerHorizontal="true" />
</RelativeLayout>
ich, warum dieser Fehler gesucht haben aufgetreten sein aber ich war nicht in der Lage, herauszufinden, warum.
Ihr numberText ist das Element RelativeLayout, es ist kein EditText. Genau wie der Fehler sagt. Was ist daran unklar? –