2012-04-19 9 views
0

私はメインのXMLファイルを作成していますが、正しいとは確信していますが、レイアウトを画面に表示する方法はわかりません。私はすべての異なるウィジェットの変数を作成し、R.id.mainと同じ変数に設定しようとしましたが、画面上に何も表示されませんでした。基本的に私はどのように画面上のxmlに入っているのかを表示する方法を理解していません。これは私のコードです、誰かが私が間違っていると説明してください。Androidレイアウトの問題 - xmlを画面に表示する

public class MinesweeperActivity extends Activity { 

private TableLayout all; 

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

    all = (TableLayout)findViewById(R.id.all); 

    } 
} 

// this is my xml 

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/all" 
android:stretchColumns="*" 
android:background="@drawable/bground"         

> 

<TableRow> 
    <TextView 
    android:id="@+id/Timer" 
    android:layout_column="0" 
    android:text="000" /> 
    <TextView 
    android:id="@+id/Counter" 
    android:layout_column="2" 
    android:text="000" /> 
    <ImageButton 
    android:id="@+id/Face" 
    android:layout_column="1" 
    android:background="@drawable/faces" 
    android:layout_height="50px" /> 
</TableRow> 
<TableRow> 
    <TextView      
     android:layout_column="0" 
     android:layout_height="50px" 
     android:layout_span="3" 
     android:padding="10dip"/> 
</TableRow> 
<TableRow> 
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/Mines" 
     android:layout_width="260px" 
     android:layout_height="260px" 
     android:gravity="bottom" 
     android:stretchColumns="*" 
     android:layout_span="3"   
     android:padding="5dip" > 
    </TableLayout> 
</TableRow>       
</TableLayout> 

答えて

1

私はあなたのコードで試したところ、logcatのエラーが**java.lang.RuntimeException: Binary XML file line #2: You must supply a layout_width attribute.**であると私は思っています。間違いはこれだと思います。あなたの問題を解決することができたら、試してみましょう。レイアウトのwidhとheight属性をテーブルレイアウトに与える必要があります。

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/all" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/bground" 
    android:stretchColumns="*" > 
+0

ありがとう、これはそれを解決しました。 – Dave

0

setContentViewでR.layout.<your xml filename>R.layout.mainを交換してください。デバッガのallの値を調べると、findViewById()は現在のコンテンツビュー内でそのIDのビューを探し、現在のコンテンツビューがR.layout.mainであるため、nullにする必要があります。 XMLリソースからアクティビティビューを構築する必要がある場合は、LayoutInflaterを使用してレイアウトリソースからビューを構築します。

+0

私のxmlファイルはmain ...と呼ばれています...また、起動時にアプリが常にクラッシュしました:/ /あなたの助けに感謝します。 – Dave

+0

アプリがクラッシュしたときのlogcatのエラーは何ですか?また、レイアウトでxml名前空間を2回定義すると、問題が発生する可能性があります。 – dwemthy

+0

私に何の意味もない行がありますが、それを貼り付けますか?私はそれを二度宣言していますか? – Dave

関連する問題