views:

65

answers:

2

Can someone help me with my Syntax/Methodology? I would like to set up a bunch of different views in a viewgroup. My code fails at the addView method.

Unfortunately, I can’t find jack for examples (which is how I learn) using the ViewGroup class online.

Thanks all.

public class TileView extends ViewGroup {
private TestClass mTestClass;
    public TileView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initViews();
    }
    public TileView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews();    
    }
    public void initViews() {
        addView(mTestClass);        
    }
    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) 
    { 
    }
}

public class TestClass extends View{

public TestClass(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public TestClass(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public TestClass(Context context) {
    super(context);
}

}

A: 

Full error listing.

08-31 14:05:45.488: ERROR/AndroidRuntime(311): FATAL EXCEPTION: main 08-31 14:05:45.488: ERROR/AndroidRuntime(311): java.lang.RuntimeException: Unable to start activity ComponentInfo{cmllc.testproject/cmllc.testproject.test}: android.view.InflateException: Binary XML file line #6: Error inflating class cmllc.testproject.TileView 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.os.Handler.dispatchMessage(Handler.java:99) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.os.Looper.loop(Looper.java:123) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.app.ActivityThread.main(ActivityThread.java:4627) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at java.lang.reflect.Method.invokeNative(Native Method) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at java.lang.reflect.Method.invoke(Method.java:521) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at dalvik.system.NativeStart.main(Native Method) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class cmllc.testproject.TileView 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.view.LayoutInflater.createView(LayoutInflater.java:513) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.app.Activity.setContentView(Activity.java:1647) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at cmllc.testproject.test.onCreate(test.java:16) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): ... 11 more 08-31 14:05:45.488: ERROR/AndroidRuntime(311): Caused by: java.lang.reflect.InvocationTargetException 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at cmllc.testproject.TileView.(TileView.java:55) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at java.lang.reflect.Constructor.constructNative(Native Method) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at java.lang.reflect.Constructor.newInstance(Constructor.java:446) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.view.LayoutInflater.createView(LayoutInflater.java:500) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): ... 21 more 08-31 14:05:45.488: ERROR/AndroidRuntime(311): Caused by: java.lang.NullPointerException 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.view.ViewGroup.addView(ViewGroup.java:1815) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): at android.view.ViewGroup.addView(ViewGroup.java:1802) 08-31 14:05:45.488: ERROR/AndroidRuntime(311): ... 25 more

A: 

First of all you should check your own layout xml file. By error message line number 6 is wrong. second problem is you didn't initialize mTestClass object in your view group source code. If you want to get children view, you can use getChildAt(int) method in view group. It is simple example,

int count = getChildCount();

for (int i = 0; i < count; i++) {
    View child = getChildAt(i);
}

And, is it full source code? I think you should implement onMeasure and onLayout method in ViewGroup and View instance.

Jang
here is an XML except starting at line 6: <cmllc.testproject.TileView android:id="@+id/game" android:layout_width="match_parent" android:layout_height="match_parent" tileSize="24" /> TileView is a viewgroup...not a view...is this my problem?
public void addView(View child, int index) { LayoutParams params = child.getLayoutParams(); ...}It is part of source of addView(View child) method in ViewGroup. The TestClass instance is not created in your code. And I couldn't understand what you want to make.
Jang