question about layout.xml and View class

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

question about layout.xml and View class

Postby codeman73 » Fri Nov 07, 2008 6:17 pm

I first got this to work with just the View components in the layout xml and no view class. Worked fine. Now I'm trying to write my own View class to handle various onFoo() events, using LunarLander as an example. In this case I was trying to display the screen dimensions because eventually I want a background image, and I need to know the dimensions for that.

When I first ran it with my View class in the layout xml, it blew up with a NPE in onSizeChanged. So at
least I knew it was getting into my View class. I commented out the info.setText line and it ran fine, but the view is blank on the screen. Why are the various TextViews and buttons not visible? I couldn't find any guidelines in the docs as to how/where to place your View class in the layout.xml.

I'd appreciate any help. Thanks.

Here's my simple layout and class:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
  5. android"
  6.  
  7.    android:orientation="vertical"
  8.  
  9.    android:layout_width="fill_parent"
  10.  
  11.    android:layout_height="fill_parent"
  12.  
  13.  
  14.  
  15.        <com.my.package.BoardView
  16.  
  17.      android:id="@+id/board"
  18.  
  19.      android:layout_width="fill_parent"
  20.  
  21.      android:layout_height="fill_parent"/>
  22.  
  23.  
  24.  
  25.         <TextView  android:id="@+id/TopText"
  26.  
  27.            android:layout_width="wrap_content"
  28.  
  29.            android:layout_height="wrap_content"
  30.  
  31.            android:text="play area"
  32.  
  33.        />
  34.  
  35.         <LinearLayout android:id="@+id/rowButtons"
  36.  
  37.                android:orientation="horizontal"
  38.  
  39.            android:layout_width="wrap_content"
  40.  
  41.            android:layout_height="wrap_content"
  42.  
  43.        >
  44.  
  45.                 <Button android:id="@+id/btnPlay"
  46.  
  47.                        android:text="Play"
  48.  
  49.                        android:layout_width="wrap_content"
  50.  
  51.                        android:layout_height="wrap_content" />
  52.  
  53.                 <Button android:id="@+id/btnInfo"
  54.  
  55.                        android:text="Info"
  56.  
  57.                        android:layout_width="wrap_content"
  58.  
  59.                        android:layout_height="wrap_content" />
  60.  
  61.                 <Button android:id="@+id/btnSetup"
  62.  
  63.                        android:text="Setup"
  64.  
  65.                        android:layout_width="wrap_content"
  66.  
  67.                        android:layout_height="wrap_content" />
  68.  
  69.         </LinearLayout>
  70.  
  71.  
  72.  
  73.         <TextView  android:id="@+id/InfoText"
  74.  
  75.            android:layout_width="wrap_content"
  76.  
  77.            android:layout_height="wrap_content"
  78.  
  79.            android:text="screen size:"
  80.  
  81.        />
  82.  
  83. </LinearLayout>
  84.  
  85.  
Parsed in 0.005 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class BoardView extends SurfaceView {
  3.  
  4.  
  5.  
  6.         public BoardView(Context context, AttributeSet attrs) {
  7.  
  8.         super(context, attrs);
  9.  
  10.         }
  11.  
  12.  
  13.  
  14.         @Override
  15.  
  16.         protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  17.  
  18.                 // TODO Auto-generated method stub
  19.  
  20.                 super.onSizeChanged(w, h, oldw, oldh);
  21.  
  22.  
  23.  
  24.                 TextView info = (TextView) findViewById(R.id.InfoText);
  25.  
  26.                 //info.setText("width : " + w + ", height : " + h);
  27.  
  28.         }
  29.  
  30.  
  31.  
  32. }
  33.  
  34.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
codeman73
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Nov 07, 2008 5:55 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 8 guests