Subclassed view crashes


Last Updated:

  1. Larsp

    Larsp New Member This Topic's Starter

    Joined:
    Nov 14, 2010
    Messages:
    3
    Likes Received:
    0
    Hi developer forum!

    Disclaimer: I'm completely new to Android development, and my java is rusty. So bear with me. I'm Using Eclipse 3.5 on a mac.

    I'm struggling to get any kind of subclassed view to work in my Android layout. What I'm trying to do, is to have a few TextViews and a subclassed SurfaceView side by side, defined in layout.xml. But the application simply crashes in the emulator when I substitute a real SurfaceView with my subclassed one.

    I'm confused, because I believe I have done the same as other examples demonstrating subclassed views do...

    1. My subclassed surfaceview, LarspPreview.java:

    Code (Text):
    1. package com.example.larsp.testing;
    2.  
    3. import android.content.Context;
    4. import android.view.SurfaceView;
    5.  
    6.  
    7. public class LarspPreview extends SurfaceView
    8. {
    9.     LarspPreview(Context context)
    10.     {
    11.         super(context);
    12.     }
    13.      
    14. }
    2. Excerpt from my main.xml layout:


    Works:


    Code (Text):
    1.         <SurfaceView
    2.             android:layout_weight="1"
    3.             android:layout_width="fill_parent"
    4.             android:layout_height="fill_parent" />
    Crashes: (android says in emulator: "... has stopped unexpectedly ...")


    Code (Text):
    1.         <com.example.larsp.testing.LarspPreview
    2.             android:layout_weight="1"
    3.             android:layout_width="fill_parent"
    4.             android:layout_height="fill_parent" />
    What am I missing? Its making me frustrated that I can't get this simple concept working!!

    Thanks!
     

    Advertisement
  2. droidotheplains

    droidotheplains Member

    Joined:
    Nov 11, 2010
    Messages:
    15
    Likes Received:
    1
    I've never had any luck at all using the emulator or the layout preview with my sub-classed Views. Would be nice if there were a way to let the emulator become familiar with your unique sub-class. Either this or we're both missing something... But each of my sub-classed View objects functions as designed on my test device (actually my working phone) with no complaints. Have you installed and tested on device?
     
  3. aguywithathing

    aguywithathing Member

    Joined:
    Oct 12, 2010
    Messages:
    18
    Likes Received:
    5
    Very simple solution, your missing 1 constructor :)

    try adding:

    Code (Text):
    1.  
    2.     LarspPreview( Context context, AttributeSet attrs )
    3.     {
    4.         super(context, attrs);
    5.     }
    6.  
    This is because there are multiple ways to create views. The above code takes attributes (these are the values you set in the XML) and assigns them to corresponding attributes of that view. The below code is for direct instantiation of the view, this is where you set your attributes manually in the code.

    Code (Text):
    1.  
    2.     LarspPreview(Context context)
    3.     {
    4.         super(context);
    5.     }
    6.  
    There is also a third constructor, again for XML but for setting base styles :

    Code (Text):
    1.  
    2.     LarspPreview(Context context, AttributeSet attrs, int defStyle)
    3.     {
    4.         super(context, attrs, defStyle);
    5.     }
    6.  
    Hope this helped.

    I'm actually currently writing a tutorial about GLSurfaceView and this is a topic is covered in that, lol.
     
  4. Larsp

    Larsp New Member This Topic's Starter

    Joined:
    Nov 14, 2010
    Messages:
    3
    Likes Received:
    0
    Thanks for the tips :)

    I tried running on the real device. Same problem.

    I tried adding the extra constructors, still doesn't work! :confused:

    So, something is still missing. Interestingly, I can use the subclassed view without crashing, if I load it directly from the activity onCreate function. This works:
    Code (Text):
    1.  
    2.         lpp = new LarspPreview(this);
    3.         setContentView(lpp);
    While it crashes when using the xml layout:
    Code (Text):
    1.  
    2.         setContentView(R.layout.main);
    Also, in other app examples, the subclassed views work just fine... There must be a link somewhere I don't understand or haven't setup. For instance, how exactly are the views defined in the layout xml instantiated? Maybe com.example.larsp.testing.LarspPreview is somehow not visible?
     
  5. aguywithathing

    aguywithathing Member

    Joined:
    Oct 12, 2010
    Messages:
    18
    Likes Received:
    5
    My mistake, Surface view needs to implement SurfaceHolder.Callback

    The following code should work:

    Code (Text):
    1.  
    2. package com.example.larsp.testing;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6.  
    7. public class Larsp extends Activity {
    8.  
    9.     @Override
    10.     public void onCreate(Bundle savedInstanceState) {
    11.         super.onCreate(savedInstanceState);
    12.         setContentView(R.layout.main);
    13.     }
    14. }
    15.  
    Code (Text):
    1.  
    2. package com.example.larsp.testing;
    3.  
    4. import android.content.Context;
    5. import android.util.AttributeSet;
    6. import android.view.SurfaceHolder;
    7. import android.view.SurfaceView;
    8.  
    9. public class LarspPreview extends SurfaceView implements SurfaceHolder.Callback {
    10.  
    11.      private SurfaceHolder mHolder;
    12.  
    13.      public LarspPreview(Context context) {
    14.          super( context );
    15.          onLoad();
    16.      }
    17.  
    18.      public LarspPreview(Context context, AttributeSet attrs) {
    19.          super( context, attrs );
    20.          onLoad();
    21.      }
    22.  
    23.      public LarspPreview(Context context, AttributeSet attrs, int defStyle) {
    24.          super( context, attrs, defStyle);
    25.          onLoad();
    26.      }
    27.      
    28.      public void onLoad()
    29.      {
    30.          mHolder = getHolder();
    31.          mHolder.addCallback(this);
    32.      }
    33.  
    34.      public void surfaceCreated(SurfaceHolder holder) {}
    35.      public void surfaceDestroyed(SurfaceHolder holder) {}
    36.      public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {}
    37. }
    38.  
    and layout :

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout
    4.         xmlns:android="http://schemas.android.com/apk/res/android"
    5.         android:layout_width="fill_parent"
    6.         android:layout_height="fill_parent">
    7.     <com.example.larsp.testing.LarspPreview
    8.         android:layout_width="fill_parent"
    9.         android:layout_height="fill_parent" />
    10. </LinearLayout>
    11.  
     
    Larsp likes this.
  6. Larsp

    Larsp New Member This Topic's Starter

    Joined:
    Nov 14, 2010
    Messages:
    3
    Likes Received:
    0
    It worked thanks!

    I guess there is more to inheriting a surfaceview than I thought...
     
  7. DKDiveDude

    DKDiveDude New Member

    Joined:
    Dec 30, 2010
    Messages:
    2
    Likes Received:
    0
    So what would I need to change in code for the below XML?

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout
    4.         xmlns:android="http://schemas.android.com/apk/res/android"
    5.         android:layout_width="fill_parent"
    6.         android:layout_height="fill_parent">
    7.  
    8.     <SurfaceView android:id="@+id/surfaceviewtest"
    9.         android:layout_width="fill_parent"
    10.         android:layout_height="fill_parent" />
    11. </LinearLayout>
    12.  
    And while I am at it, probably needs a new thread, how do I draw with code on this layout;

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout
    4.         xmlns:android="http://schemas.android.com/apk/res/android"
    5.         android:layout_width="fill_parent"
    6.         android:layout_height="fill_parent">
    7.     <View android:id="@+id/viewtest"
    8.         android:layout_width="fill_parent"
    9.         android:layout_height="fill_parent" />
    10. </LinearLayout>
    11.  
    [/code]
     

Share This Page

Loading...