简体   繁体   中英

Android Layout problems - displaying xml on screen

Ok so i've written my main xml file and i'm pretty sure its correct, but I dont know how to display my layout on the screen. I tried creating variables for all the different widgets and setting them to the same as in the R.id.main but it didnt show anything on screen. basically i don't understand how to display what ive got in xml on screen. This is my code, please can someone explain where i'm going wrong.

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>

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="*" >

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM