FindViewByIDを呼び出すことで、いくつかのコンポーネントを見つけることができませんでした

StackOverflow https://stackoverflow.com/questions/9320873

  •  26-10-2019
  •  | 
  •  

質問

FindViewByidは、IDが存在していても、ボタンとRadiusBarを取得できません。 SaveButtonまたはRadiusBarを開始すると、コードがクラッシュします。

行為:

    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ファイル:

<?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>

私はAndroidで新しいです。私は古い学校のコメントアウト方法やDDMSを使用しましたが、なぜこれが起こっているのかわからなかった。助けてくれてありがとう。

役に立ちましたか?

解決

「クラッシュ」と言うときは、「...これがエラーメッセージを使用したログ出力です」と言う必要があります。とにかく、コードがコンパイルされると、リソースIDが確かに存在します。ただし、Androidを使用した一般的なビルドシステムエラーは、リソースIDができることです。 違う いくつかのXMLを並べ替えた後。エラーがClassCastExceptionであることがわかると思います。解決策:リポジトリをきれいにします(例、 ant clean, 、またはEclipseのボタン/メニュー)をゼロから再コンパイルします。

他のヒント

同様の問題が発生したときはいつでも、Eclipseで次のメニューを使用することでそれを解決することができます:Project-> Clean

Androidプロジェクトが自動的に生成するクラスを見ることができます

この場合、Eclipseは少し賢すぎます。レイアウトの一部であるIDが見つかります。 setContentView()レイアウトとして割り当てたものだけではありません。

したがって、有効なIDがありますが、レイアウトにIDが含まれていない場合、実行時には有効ではありません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top