KB-20071208-030
From Triled Wiki
- Status: respond received
- author: Rick Genter
- link:
- Description: TimePicker is not working in XML file
- Steps to reproduce:
Make layout in which TimePicker nested within a LinearLayout, that is in turn nested within another LinearLayout.
code example (from Rick Genter post):
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- the first item is the time control -->
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/MealTime_Label"/>
<TimePicker id="@+id/MealTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:showSetButton="false"/>
</LinearLayout>
<!-- other stuff (see original post) -->
</LinearLayout>
Unexpected result: java.lang.RuntimeException: Binary XML file line #125: You must supply a layout_width attribute.
Thus Activity is not started. Replacing LinearLayout with TableLayout makes Activity start, but the ids of the nested views are not accessible.
code example (from Rick Genter post):
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TimePicker id="@+id/Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow id="@+id/ButtonRow">
<Button id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"/>
<!-- other stuff skipped, see original post -->
</TableRow>
</TableLayout>
Test3.java:
package test.test3;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TableRow;
public class Test3 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.Button1);
if (button1 == null) {
Log.e("Test3", "Couldn't find Button1 view by id");
TableRow row = (TableRow) findViewById(R.id.ButtonRow);
button1 = (Button) row.getChildAt(0);
}
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
finish();
}
});
// skipped other code, see original post
}
}
Unexpected result: findViewById() returns null. "Couldn't find Button1 view by id" is logged.
