0

RelativeLayout後でやりたいことのテストとして、これを非常に簡単に作成しました。現在のところ、 、second、 の3 つのボタンが互いに相対的に配置されているはずです。alphamode

問題が 2 つあります。1 つ目は、XML レイアウト ファイルに何か問題があり、徹底的にチェックしたので何が原因かわかりません。これにより、レイアウトの 10 行目と 11 行目でエラーが発生します。R第 2 に、通常は Android アクティビティにあるため、生成されたファイルはありません。これにより、R ファイルから何かを呼び出そうとするため、メイン コードの 11 行目でエラーが発生します。

私のコードは次のとおりです。

レイアウト:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/alpha"
        android:layout_toLeftOf="@id/mode"
        android:text="@string/second"
        android:layout_alignParentLeft="true" />
    <Button
        android:id="@+id/alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/second"
        android:layout_alignParentLeft="true"
        android:text="@string/alpha" />
    <Button
        android:id="@+id/mode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/second"
        android:layout_alignParentLeft="true"
        android:text="@string/mode" />
</RelativeLayout>

メインActivityコード:

package com.example.TI84;

import android.app.Activity;
import android.os.Bundle;

public class TI84GraphingCalculatorActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

ご協力ありがとうございました!

4

4 に答える 4

3

RelativeLayouts は、その名前が示すように、要素を相対方式で編成します。したがって、ファイル内の上位にあるウィジェットから下位にあるウィジェットへの参照を作成することはできません。このレイアウトは機能します:

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

    <Button
        android:id="@+id/mode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/alpha" />

    <Button
        android:id="@+id/second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/mode"
        android:text="@string/second" />

    <Button
        android:id="@+id/alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/mode"
        android:layout_below="@id/mode"
        android:text="@string/alpha" />

</RelativeLayout>

XML ファイルにエラーがある場合は R.java が生成されないため、R.java を取得できませんでした。

また、これを要求していませんが、元の XML ファイルには循環依存関係がありました。これは、A が B の左に、B が A の右にあるように、2 つのウィジェットが無限ループを作成するような方法で相互に参照することを意味します。

于 2012-06-25T21:40:22.990 に答える
2

xml ファイルにエラーが含まれている場合、R クラスは生成されません。あなたのエラーはこれです:

android:layout_above="@id/alpha"

置く:

android:layout_above="@+id/alpha"
于 2012-06-25T21:29:16.540 に答える
1

エラーが発生する理由は、ファイル内のその時点で定義されていないIDを参照しているためです。ファイル内の要素の上に定義されているIDを基準にしてのみレイアウトできます。

R.javaXMLファイルのエラーによりビルドが失敗し、ビルド中にファイルが生成されるため、ファイルはありません。

于 2012-06-25T21:31:01.023 に答える
1

10行目でこれを試してください

android:layout_above="@+id/alpha"

そしてこれは11で

android:layout_toLeftOf="@+id/mode"
于 2012-06-25T21:28:55.453 に答える