0

好的,所以我已经编写了我的主 xml 文件,我很确定它是正确的,但我不知道如何在屏幕上显示我的布局。我尝试为所有不同的小部件创建变量并将它们设置为与 R.id.main 中的相同,但它没有在屏幕上显示任何内容。基本上我不明白如何在屏幕上显示我在 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>
4

2 回答 2

1

I tried with your code and i found the error in logcat is **java.lang.RuntimeException: Binary XML file line #2: You must supply a layout_width attribute.** I think the mistake is this..Try once it may solve your problem.You must give layout widh and height attributes to the table layout.

<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="*" >
于 2012-04-20T04:16:54.227 回答
0

Replace R.layout.main with R.layout.<your xml filename> in setContentView. If you inspect the value of all in the debugger it should be null because findViewById() looks in the current content view for a view with that id, and your current content view is R.layout.main. If you need to build a view in your Activity from an xml resource use a LayoutInflater to build a view from a layout resource.

于 2012-04-19T20:07:21.007 回答