Quantcast

Beginner problem with definition of custom views

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Beginner problem with definition of custom views

Igor Pietraszko
I am trying to port an example from java hat creates a custom view but am getting stack on an seemingly trivial problem - ClassNotFoundException when inflating the XML layout!  Here is the sample code, accompanying XML and Exception stack trace.  To simplify any linking/compilation issues I even moved MainActivityGroup into the same file as MainActivity (had it separate initially). I am not sure if that is an environment issue or what.  Please help.  Thanks.

Code:
using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Util;

namespace Stackables
{
[Activity (MainLauncher = true)]
public class MainActivity : Activity
{
#region fields
private MainViewGroup _mainView;
#endregion
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// This will force full screen without a title bar
RequestWindowFeature(WindowFeatures.NoTitle);
Window.SetFlags(WindowManagerFlags.Fullscreen,
WindowManagerFlags.Fullscreen);

// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
_mainView = (MainViewGroup) FindViewById(Resource.Id.mainView);
}
}
public class MainViewGroup : ViewGroup
{
#region cTor
public MainViewGroup (Context context) : base (context)
{
Initialize ();
}
public MainViewGroup (Context context, IAttributeSet attrs) :
base (context, attrs)
{
Initialize ();
}

public MainViewGroup (Context context, IAttributeSet attrs, int defStyle) :
base (context, attrs, defStyle)
{
Initialize ();
}
#endregion
private void Initialize ()
{
}
protected override void OnLayout (bool changed, int l, int t, int r, int b)
{
//base.OnLayout(changed, l, t, r, b);
}
protected override void OnMeasure (int widthMeasureSpec, int heightMeasureSpec)
{
// read up on UNSPECIFIED, AT_MOST and EXACTLY flags
int parentWidth = MeasureSpec.GetSize(widthMeasureSpec);
int parentHeight = MeasureSpec.GetSize(heightMeasureSpec);
this.SetMeasuredDimension(parentWidth - 20, parentHeight - 20);
}
}
}

Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:gravity="center"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<Stackables.MainViewGroup
android:id="@+id/mainView"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

Exception trace:
I/monodroid( 3536): Activating instance of type Stackables.MainActivity
I/monodroid( 3536): signature 
I/MonoDroid( 3536): UNHANDLED EXCEPTION: Android.Views.InflateException: Exception of type 'Android.Views.InflateException' was thrown.
I/MonoDroid( 3536): at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) <0x00088>
I/MonoDroid( 3536): at Android.App.Activity.SetContentView (int) <0x0020b>
I/MonoDroid( 3536): at Stackables.MainActivity.OnCreate (Android.OS.Bundle) <0x00063>
I/MonoDroid( 3536): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <0x0005f>
I/MonoDroid( 3536): at (wrapper dynamic-method) object.996f8f21-0b62-4961-aa95-507be18b3555 (intptr,intptr,intptr) <0x00033>
I/MonoDroid( 3536): 
I/MonoDroid( 3536):   --- End of managed exception stack trace ---
I/MonoDroid( 3536): android.view.InflateException: Binary XML file line #9: Error inflating class Stackables.MainViewGroup
I/MonoDroid( 3536): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
I/MonoDroid( 3536): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
I/MonoDroid( 3536): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
I/MonoDroid( 3536): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
I/MonoDroid( 3536): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
I/MonoDroid( 3536): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
I/MonoDroid( 3536): at android.app.Activity.setContentView(Activity.java:1657)
I/MonoDroid( 3536): at stackables.MainActivity.n_onCreate(Native Method)
I/MonoDroid( 3536): at stackables.MainActivity.onCreate(MainActivity.java:25)
I/MonoDroid( 3536): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
I/MonoDroid( 3536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
I/MonoDroid( 3536): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
I/MonoDroid( 3536): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
I/MonoDroid( 3536): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
I/MonoDroid( 3536): at android.os.Handler.dispatchMessage(Handler.java:99)
I/MonoDroid( 3536): at android.os.Looper.loop(Looper.java:123)
I/MonoDroid( 3536): at android.app.ActivityThread.main(ActivityThread.java:3683)
I/MonoDroid( 3536): at java.lang.reflect.Method.invokeNative(Native Method)
I/MonoDroid( 3536): at java.lang.reflect.Method.invoke(Method.java:507)
I/MonoDroid( 3536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
I/MonoDroid( 3536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
I/MonoDroid( 3536): at dalvik.system.NativeStart.main(Native Method)
I/MonoDroid( 3536): Caused by: java.lang.ClassNotFoundException: Stackables.MainViewGroup in loader dalvik.system.PathClassLoader[/data/app/stackables.stackables-1.apk]
I/MonoDroid( 3536): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
I/MonoDroid( 3536): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
I/MonoDroid( 3536): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
I/MonoDroid( 3536): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
I/MonoDroid( 3536): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
I/MonoDroid( 3536): ... 21 more
E/mono    ( 3536): 
E/mono    ( 3536): Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
E/mono    ( 3536):   at Android.Runtime.JNIEnv.NewObject (IntPtr jclass, IntPtr jmethod) [0x00000] in <filename unknown>:0 
E/mono    ( 3536):   at Java.IO.StringWriter..ctor () [0x00000] in <filename unknown>:0 
E/mono    ( 3536):   at Java.Lang.Throwable.get_StackTrace () [0x00000] in <filename unknown>:0 
E/mono    ( 3536):   at System.Exception.ToString () [0x00000] in <filename unknown>:0 

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Beginner problem with definition of custom views

Jonathan Pobst
Because the resources are processed by Java, you have to use the Java
name/capitalization of your class in the layout file instead of the .NET
one.

Some more information is available here:

http://mono-android.net/Documentation/Guides/Using_Custom_Views_in_a_Layout

Jonathan


On 4/22/2011 8:39 AM, Igor Pietraszko wrote:

> I am trying to port an example from java hat creates a custom view but
> am getting stack on an seemingly trivial problem -
> ClassNotFoundException when inflating the XML layout!  Here is the
> sample code, accompanying XML and Exception stack trace.  To simplify
> any linking/compilation issues I even moved MainActivityGroup into the
> same file as MainActivity (had it separate initially). I am not sure if
> that is an environment issue or what.  Please help.  Thanks.
>
> Code:
> using System;
>
> using Android.App;
> using Android.Content;
> using Android.Runtime;
> using Android.Views;
> using Android.Widget;
> using Android.OS;
> using Android.Util;
>
> namespace Stackables
> {
> [Activity (MainLauncher = true)]
> public class MainActivity : Activity
> {
> #region fields
> private MainViewGroup _mainView;
> #endregion
> protected override void OnCreate (Bundle bundle)
> {
> base.OnCreate (bundle);
> // This will force full screen without a title bar
> RequestWindowFeature(WindowFeatures.NoTitle);
> Window.SetFlags(WindowManagerFlags.Fullscreen,
> WindowManagerFlags.Fullscreen);
>
> // Set our view from the "main" layout resource
> SetContentView (Resource.Layout.Main);
> _mainView = (MainViewGroup) FindViewById(Resource.Id.mainView);
> }
> }
> public class MainViewGroup : ViewGroup
> {
> #region cTor
> public MainViewGroup (Context context) : base (context)
> {
> Initialize ();
> }
> public MainViewGroup (Context context, IAttributeSet attrs) :
> base (context, attrs)
> {
> Initialize ();
> }
>
> public MainViewGroup (Context context, IAttributeSet attrs, int defStyle) :
> base (context, attrs, defStyle)
> {
> Initialize ();
> }
> #endregion
> private void Initialize ()
> {
> }
> protected override void OnLayout (bool changed, int l, int t, int r, int b)
> {
> //base.OnLayout(changed, l, t, r, b);
> }
> protected override void OnMeasure (int widthMeasureSpec, int
> heightMeasureSpec)
> {
> // read up on UNSPECIFIED, AT_MOST and EXACTLY flags
> int parentWidth = MeasureSpec.GetSize(widthMeasureSpec);
> int parentHeight = MeasureSpec.GetSize(heightMeasureSpec);
> this.SetMeasuredDimension(parentWidth - 20, parentHeight - 20);
> }
> }
> }
>
> Main.axml
> <?xml version="1.0" encoding="utf-8"?>
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
> android:id="@+id/root"
> android:gravity="center"
>      android:orientation="horizontal"
>      android:layout_width="fill_parent"
>      android:layout_height="fill_parent">
> <Stackables.MainViewGroup
> android:id="@+id/mainView"
> android:orientation="horizontal"
> android:layout_width="fill_parent"
> android:layout_height="fill_parent"
> />
> </LinearLayout>
>
> Exception trace:
> I/monodroid( 3536): Activating instance of type Stackables.MainActivity
> I/monodroid( 3536): signature
> I/MonoDroid( 3536): UNHANDLED EXCEPTION: Android.Views.InflateException:
> Exception of type 'Android.Views.InflateException' was thrown.
> I/MonoDroid( 3536): at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod
> (intptr,intptr,intptr,Android.Runtime.JValue[]) <0x00088>
> I/MonoDroid( 3536): at Android.App.Activity.SetContentView (int) <0x0020b>
> I/MonoDroid( 3536): at Stackables.MainActivity.OnCreate
> (Android.OS.Bundle) <0x00063>
> I/MonoDroid( 3536): at
> Android.App.Activity.n_OnCreate_Landroid_os_Bundle_
> (intptr,intptr,intptr) <0x0005f>
> I/MonoDroid( 3536): at (wrapper dynamic-method)
> object.996f8f21-0b62-4961-aa95-507be18b3555 (intptr,intptr,intptr) <0x00033>
> I/MonoDroid( 3536):
> I/MonoDroid( 3536):   --- End of managed exception stack trace ---
> I/MonoDroid( 3536): android.view.InflateException: Binary XML file line
> #9: Error inflating class Stackables.MainViewGroup
> I/MonoDroid( 3536): at
> android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
> I/MonoDroid( 3536): at
> android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
> I/MonoDroid( 3536): at
> android.view.LayoutInflater.inflate(LayoutInflater.java:408)
> I/MonoDroid( 3536): at
> android.view.LayoutInflater.inflate(LayoutInflater.java:320)
> I/MonoDroid( 3536): at
> android.view.LayoutInflater.inflate(LayoutInflater.java:276)
> I/MonoDroid( 3536): at
> com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
> I/MonoDroid( 3536): at
> android.app.Activity.setContentView(Activity.java:1657)
> I/MonoDroid( 3536): at stackables.MainActivity.n_onCreate(Native Method)
> I/MonoDroid( 3536): at
> stackables.MainActivity.onCreate(MainActivity.java:25)
> I/MonoDroid( 3536): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
> I/MonoDroid( 3536): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
> I/MonoDroid( 3536): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
> I/MonoDroid( 3536): at
> android.app.ActivityThread.access$1500(ActivityThread.java:117)
> I/MonoDroid( 3536): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
> I/MonoDroid( 3536): at android.os.Handler.dispatchMessage(Handler.java:99)
> I/MonoDroid( 3536): at android.os.Looper.loop(Looper.java:123)
> I/MonoDroid( 3536): at
> android.app.ActivityThread.main(ActivityThread.java:3683)
> I/MonoDroid( 3536): at java.lang.reflect.Method.invokeNative(Native Method)
> I/MonoDroid( 3536): at java.lang.reflect.Method.invoke(Method.java:507)
> I/MonoDroid( 3536): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
> I/MonoDroid( 3536): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
> I/MonoDroid( 3536): at dalvik.system.NativeStart.main(Native Method)
> I/MonoDroid( 3536): Caused by: java.lang.ClassNotFoundException:
> Stackables.MainViewGroup in loader
> dalvik.system.PathClassLoader[/data/app/stackables.stackables-1.apk]
> I/MonoDroid( 3536): at
> dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
> I/MonoDroid( 3536): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
> I/MonoDroid( 3536): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
> I/MonoDroid( 3536): at
> android.view.LayoutInflater.createView(LayoutInflater.java:471)
> I/MonoDroid( 3536): at
> android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
> I/MonoDroid( 3536): ... 21 more
> E/mono    ( 3536):
> E/mono    ( 3536): Unhandled Exception: System.NullReferenceException:
> Object reference not set to an instance of an object
> E/mono    ( 3536):   at Android.Runtime.JNIEnv.NewObject (IntPtr jclass,
> IntPtr jmethod) [0x00000] in <filename unknown>:0
> E/mono    ( 3536):   at Java.IO.StringWriter..ctor () [0x00000] in
> <filename unknown>:0
> E/mono    ( 3536):   at Java.Lang.Throwable.get_StackTrace () [0x00000]
> in <filename unknown>:0
> E/mono    ( 3536):   at System.Exception.ToString () [0x00000] in
> <filename unknown>:0
>
>
>
> _______________________________________________
> Monodroid mailing list
> [hidden email]
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
[hidden email]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid
Loading...