2012-04-19 64 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.**我认为错误是this..Try一旦它可以解决您的problem.You必须给布局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 2012-04-20 13:04:41

0

替换R.layout.main用的setContentView R.layout.<your xml filename>。如果在调试器中检查all的值,它应该为空,因为findViewById()在当前内容视图中查找具有该ID的视图,而当前内容视图为R.layout.main。如果您需要在xml资源的Activity中构建视图,请使用LayoutInflater从布局资源构建视图。

+0

我的xml文件被称为main.xml虽然...此外,应用程序总是崩溃时,我开始它:/ - 感谢您的帮助顺便说一句。 – Dave 2012-04-19 20:19:27

+0

当应用程序崩溃时,logcat中的错误是什么?另外,它看起来像是在布局中定义了两次xml命名空间,这可能会导致一些问题。 – dwemthy 2012-04-19 21:02:31

+0

那里有很多线条对我来说毫无意义,我应该粘贴它吗?我还在哪里宣布它两次? – Dave 2012-04-19 22:35:54