To inflate a View from XML you need to implement the constructors that take
an AttributeSet. The constructor that only takes a Context is not invoked
when inflating from XML.

On Fri, Mar 25, 2011 at 10:06 PM, Atul Prakash <[email protected]>wrote:

> Dear Friends,
>
> I am trying to inflate View class from xml resource. Moreover i am
> following all the conventions of Building Custom Components given on
> Android developer site. I have defined View class as subclass of My
> Activity class which is as follows:
>
>    public class MyApp extends Activity {
>    @Override
>    public void onCreate(Bundle savedInstanceState) {
>        super.onCreate(savedInstanceState);
>        setContentView(R.layout.main);
>    }
>     public static class MyAppView extends View {
>                Bitmap menu;
>                int menudemo[];
>                public MyAppView(Context context) {
>                        super(context);
>                        // TODO Auto-generated constructor stub
>                        menudemo = new int[]
> {R.drawable.menu,R.drawable.icon};
>                }
>                @Override
>                public void onDraw(Canvas canvas) {
>                        Resources res = getResources();
>                        canvas.drawBitmap(BitmapFactory.decodeResource(res,
> menudemo[0]),
> 0, 0, null);
>                        System.gc();
>                        invalidate();
>                }
>        }
> }
> My main.xml is as follows:
> <?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"
>    >
> <view class="com.myapp.MyApp$MyAppView"
>    android:id="@+id/my"
>    android:layout_width="fill_parent"
>    android:layout_height="fill_parent"
>    />
> <TextView
>    android:layout_width="fill_parent"
>    android:layout_height="wrap_content"
>    android:text="@string/hello"
>    />
> </LinearLayout>
>
> I do not know what is wrong with my coding, but when i am running this
> application i am getting run time exception like:
>
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107): FATAL EXCEPTION: main
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.myapp/com.myapp.MyApp}:
> android.view.InflateException: Binary XML file line #7: Error
> inflating class com.myapp.MyApp$MyAppView
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
> 2663)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
> 2679)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.app.ActivityThread.access$2300(ActivityThread.java:125)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.os.Handler.dispatchMessage(Handler.java:99)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.os.Looper.loop(Looper.java:123)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.app.ActivityThread.main(ActivityThread.java:4627)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> java.lang.reflect.Method.invokeNative(Native Method)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> java.lang.reflect.Method.invoke(Method.java:521)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> com.android.internal.os.ZygoteInit
> $MethodAndArgsCaller.run(ZygoteInit.java:868)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> dalvik.system.NativeStart.main(Native Method)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107): Caused by:
> android.view.InflateException: Binary XML file line #7: Error
> inflating class com.myapp.MyApp$MyAppView
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.view.LayoutInflater.createView(LayoutInflater.java:503)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.view.LayoutInflater.inflate(LayoutInflater.java:407)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.view.LayoutInflater.inflate(LayoutInflater.java:320)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.view.LayoutInflater.inflate(LayoutInflater.java:276)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
>
> com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:
> 198)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.app.Activity.setContentView(Activity.java:1647)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> com.myapp.MyApp.onCreate(MyApp.java:23)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
> 1047)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
> 2627)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     ... 11 more
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107): Caused by:
> java.lang.NoSuchMethodException: MyAppView(Context,AttributeSet)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> java.lang.Class.getMatchingConstructor(Class.java:660)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> java.lang.Class.getConstructor(Class.java:477)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     at
> android.view.LayoutInflater.createView(LayoutInflater.java:475)
> 03-25 12:28:50.842: ERROR/AndroidRuntime(1107):     ... 21 more
> 03-25 12:28:50.882: WARN/ActivityManager(71):   Force finishing
> activity com.myapp/.MyApp
> 03-25 12:28:51.392: WARN/ActivityManager(71): Activity pause timeout
> for HistoryRecord{441c18d0 com.myapp/.MyApp}
> 03-25 12:29:01.981: WARN/ActivityManager(71): Activity destroy timeout
> for HistoryRecord{441c18d0 com.myapp/.MyApp}
> 03-25 12:33:50.912: INFO/Process(1107): Sending signal. PID: 1107 SIG:
> 9
>
> Please help me to solve this RunTime Exception.
>
> Thanks & Regards.
>
> Atul Prakash Singh
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Romain Guy
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to