Frage

FindViewById kann keine Taste und eine Radiusleiste abrufen, obwohl ihre ID existiert. Code stürzt ab, wenn SaveButton oder Radiusbar initiiert werden.

Handlung:

    private Button saveButton;
    private EditText titleText;
    private SeekBar radiusBar;
    private TextView radiusValue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        titleText = (EditText) findViewById(R.id.title);
        radiusValue = (TextView) findViewById(R.id.radiusValue);
        saveButton = (Button) findViewById(R.id.confirm);
    radiusBar = (SeekBar) findViewById(R.id.radiusBar);

XML -Datei:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="3dp"
    android:text="@string/title"
    android:textColor="#00ffff"
    android:textSize="20sp"
    android:textStyle="bold" />

<EditText
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="3dp" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/myLocationText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="3dp"
    android:text="@string/hello"
    android:textColor="#ffffff"
    android:textSize="15sp" />

<com.google.android.maps.MapView
    android:id="@+id/myMapView"
    android:layout_width="fill_parent"
    android:layout_height="300dp"
    android:apiKey="my google maps api key"
    android:clickable="true"
    android:enabled="true"
    android:padding="5dp" >
</com.google.android.maps.MapView>

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/radiusText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:paddingLeft="3dip"
        android:paddingRight="3dip"
        android:text="@string/proximity"
        android:textColor="#00ffff"
        android:textSize="20sp"
        android:textStyle="bold" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/radiusText" >

        <TextView
            android:id="@+id/radiusValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="3dip"
            android:paddingRight="3dip"
            android:text="@string/radius"
            android:textColor="#00ffff"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

    <SeekBar
        android:id="@+id/radiusBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/radiusText"
        android:max="1000"
        android:padding="5dip"
        android:soundEffectsEnabled="true" />

    <Button
        android:id="@+id/confirm"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/radiusBar"
        android:padding="3dp"
        android:text="@string/confirm"
        android:textSize="20sp" />
</RelativeLayout>

Ich bin neu bei Android. Obwohl ich Old School Kommentare oder DDMs verwendet habe, konnte ich nicht herausfinden, warum dies geschieht. danke für die Hilfe.

War es hilfreich?

Lösung

Wenn Sie "Crash" sagen, sollten Sie weiter sagen "... und dies ist die Protokollausgabe mit der Fehlermeldung". Wie auch immer, die Ressourcen -IDs existieren sicherlich, wie Ihr Code kompiliert. Ein gängiger Build -Systemfehler bei Android ist jedoch, dass Ihre Ressourcen -IDs sein können falsch Nachdem Sie einige XML neu ordnen. Ich denke, Sie werden feststellen, dass der Fehler eine ClassCastException ist. Die Lösung: Reinigen Sie Ihr Repo (z. B. mit ant clean, oder ein Schaltfläche/ein Menü in Sonnenfinsternis) und alles von Grund auf neu kompilieren.

Andere Tipps

Immer wenn ich ein ähnliches Problem hatte, konnte ich es normalerweise lösen, indem ich das folgende Menü in Eclipse: Project-> Clean verwendete

Sie können sich die Klasse ansehen, die Android -Projekte automatisch generieren

Eclipse ist in diesem Fall ein bisschen "zu" intelligent. Es wird eine ID finden, die Teil eines beliebigen Layouts ist. Nicht nur das, das Sie als SetContentView () Layout zugewiesen haben.

Sie haben also eine gültige ID, die zur Laufzeit jedoch nicht gültig ist, wenn das Layout die ID nicht enthält.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top