0

私はAndroidアプリの開発に非常に慣れていません。しかし、私の最初のプロジェクトの1つで、エラーが発生しました。xmlコードで導入されたがfindViewById()見つかりません。Id

これが私のJavaコードです:

package com.example.fuzzylogic;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
int counter;
Button add, sub;
TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        counter = 0;
        add = (Button) findViewById(R.id.bAdd);
        sub = (Button) findViewById(R.id.bSubtract);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

これが私のxmlコードです:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/tvDisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Your total is 0"
    android:textSize="45dp" />

<Button
    android:id="@+id/bAdd"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvDisplay"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="add one"
    android:textSize="25dp" />

<Button
    android:id="@+id/bSubtract"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/bAdd"
    android:layout_below="@+id/bAdd"
    android:layout_marginTop="23dp"
    android:text="substract one"
    android:textSize="25dp" />



</RelativeLayout>
4

3 に答える 3

1

私のfindViewById()は、xmlコードで導入されたIDを見つけられません。

したがって、これにより次のことが発生する可能性があります。

  • XMLファイルのエラーがR.java正しく生成されない
  • 編集したすべてのxmlファイルを保存していません(ctrl + s)

したがって、プロジェクトにエラーがないことを確認し、プロジェクトをクリーンアップして再構築してください。

于 2013-03-12T18:04:36.057 に答える
0

まず、xmlファイルを保存して再構築する必要があります

新しいファイルを追加したり、リソースファイルに変更を加えたりする場合は、プロジェクトを再構築することをお勧めします。これにより、行った変更に対応してR.javaファイルが変更されます。

findViewById()渡すIDには、レイアウトxmlファイルのIDではありません。実際には、R.javaファイル内のリソースオブジェクトの整数表現です。

注:Eclipseを使用している場合は、自動的にビルドオプションが選択されています

于 2013-03-12T18:15:21.480 に答える
0

同じ問題が発生しました。問題が発生した理由は、追加した新しいXmlファイルリソースでRクラスが更新されなかったためです。だから私はプロジェクトを再構築し、ブームの問題は解決しました。

したがって、プロジェクトを再構築すると役立つ場合があります。

于 2018-01-04T06:57:31.813 に答える