简体   繁体   中英

View in Activity Causes Crash

I am trying to use the fullscreen activity layout. I want the main view to be a custom view. When I try to add anything creating new view class to the fullscreen activity class it causes the program to crash when run in the emulator.

Do I have to tell the xml file that it is a custom class? Any help or pointers would be appreciated.

error:02-05 10:11:06.231: E/AndroidRuntime(823): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.unibitri.zoobies/com.unibitri.zoobies.ZoobiesMain}: android.view.InflateException: Binary XML file line #14: Error inflating class com.unibitri.zoobies.ZooView

Not sure if this is what you mean... You must reference your custom view correctly in your android xml file for the layout. Like this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/framelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<packagename.CustomView
  android:id="@+id/customView"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />
</FrameLayout>

Where packagename is something like com.example.myapplication and CustomView is the class name of your custom view

EDIT: Make sure your constructor for CustomView takes both arguments Context context, AttributeSet attr

    public CustomView(Context context, AttributeSet attr) {
        super(context, attr);

    }
  • check your logcat, it will give you a hint on what happens
  • check your custom view code and activity code according to the log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM