how to use custom views in layout xml

17,086

Solution 1

You have a NullPointerException in one of your constructors. I can not exactly tell which one, as the StackTrace says line 27, which is an empty line in the code you posted.

You should re-run your code, check the first line of the stack trace in your code. I would guess it's this line:

duration = movie.duration();

and the movie object is null.

Solution 2

The problem is not with your code but displaying custom view in Graphical Layout. Drawing views rely on device features. To see your custom view properly you have to use the isInEditMode() method in your custom view implementation. Based on that method, you have to implement alternative initialization and drawing.

Solution 3

I think you want your xml file object to be like this:

<com.spazio.digitale.GIFView android:id="@+id/gIFView1"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity="center" />

assuming that your package is com.spazio.digitale which I'm not totally sure of from what you've posted.

Share:
17,086
Jayyrus
Author by

Jayyrus

I'm an Italian Software Engineer. I like very much to learn and study new technologies

Updated on June 04, 2022

Comments

  • Jayyrus
    Jayyrus almost 2 years

    I've created a custom layout to show gif animated image. Here is the code:

    public class GIFView extends View{        
    private Movie movie;  
    private InputStream is;  
    private long moviestart;  
    private long duration;
    
    public GIFView(Context context) {  
        super(context);
        is=getResources().openRawResource(R.drawable.anim_cerca);  
        movie=Movie.decodeStream(is);
        duration = movie.duration();
    }  
    
    public GIFView(Context context,AttributeSet set){
        super(context,set);
        is=getResources().openRawResource(R.drawable.anim_cerca);  
        movie=Movie.decodeStream(is);
        duration = movie.duration();
    }
    
    public GIFView(Context context,AttributeSet set,int def){
        super(context,set,def);
        is=getResources().openRawResource(R.drawable.anim_cerca);  
        movie=Movie.decodeStream(is);
        duration = movie.duration();
    }
    
    @Override  
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        long now=android.os.SystemClock.uptimeMillis();
        Paint p = new Paint();
        p.setAntiAlias(true);
        if (moviestart == 0) 
            moviestart = now;
    
        int relTime = (int)((now - moviestart) % duration);
    
        movie.setTime(relTime);
        movie.draw(canvas,0,0);
    
    
        this.invalidate();
    }                         
    }  
    

    This is the xml where i put this object:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent" android:background="@drawable/sfondo">
    <LinearLayout android:id="@+id/linearLayout2"
        android:layout_height="wrap_content" android:layout_width="match_parent"
        android:orientation="vertical">
        <LinearLayout android:id="@+id/linearLayout1"
            android:layout_height="fill_parent" android:weightSum="1"
            android:layout_width="fill_parent" android:orientation="horizontal">
            <ImageView android:layout_height="73dp" android:id="@+id/imageView1"
                android:layout_weight="0.25" android:src="@drawable/trovachiavi"
                android:layout_width="256dip"></ImageView>
            <ImageButton android:id="@+id/infoButton"
                android:background="@null" android:layout_height="47dp"
                android:layout_marginLeft="10dip" android:layout_weight="0.25"
                android:layout_marginTop="15dip" android:src="@drawable/info_mini"
                android:layout_width="47dp"></ImageButton>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout android:id="@+id/layoutGIF"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:orientation="vertical">
        <com.digit.GIFView android:id="@+id/gIFView1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_gravity="center"></com.digit.GIFView>
    </LinearLayout>
    <ImageButton android:id="@+id/avvia_cerca"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:layout_marginTop="10dp" android:layout_gravity="center|bottom"
        android:background="@null"></ImageButton>
    </LinearLayout>
    

    When i go in the Graphical Layout zone of xml, it appears an error:

    The following classes could not be instantiated:
    -  (Open Class, Show Error Log)
    See the Error Log (Window > Show View) for more details.
    Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
    

    and in Error log appears these:

    java.lang.NullPointerException
    at com.digit.GIFView.<init>(GIFView.java:27)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:396)
    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:164)
    at com.android.layoutlib.bridge.android.BridgeInflater.loadCustomView(BridgeInflater.java:205)
    at com.android.layoutlib.bridge.android.BridgeInflater.createViewFromTag(BridgeInflater.java:133)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:83)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:702)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:86)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:702)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:367)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:324)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:321)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:380)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1310)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1075)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:901)
    at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.partActivated(LayoutEditor.java:416)
    at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.partBroughtToTop(LayoutEditor.java:425)
    at org.eclipse.ui.internal.PartListenerList$2.run(PartListenerList.java:87)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.core.runtime.Platform.run(Platform.java:888)
    at org.eclipse.ui.internal.PartListenerList.fireEvent(PartListenerList.java:57)
    at org.eclipse.ui.internal.PartListenerList.firePartBroughtToTop(PartListenerList.java:85)
    at org.eclipse.ui.internal.PartService.firePartBroughtToTop(PartService.java:208)
    at org.eclipse.ui.internal.WorkbenchPagePartList.firePartBroughtToTop(WorkbenchPagePartList.java:76)
    at org.eclipse.ui.internal.WorkbenchPagePartList.fireActiveEditorChanged(WorkbenchPagePartList.java:52)
    at org.eclipse.ui.internal.PartList.setActiveEditor(PartList.java:162)
    at org.eclipse.ui.internal.WorkbenchPage.makeActiveEditor(WorkbenchPage.java:1281)
    at org.eclipse.ui.internal.WorkbenchPage.setActivePart(WorkbenchPage.java:3530)
    at org.eclipse.ui.internal.WorkbenchPage.requestActivation(WorkbenchPage.java:3077)
    at org.eclipse.ui.internal.PartPane.requestActivation(PartPane.java:279)
    at org.eclipse.ui.internal.EditorPane.requestActivation(EditorPane.java:98)
    at org.eclipse.ui.internal.PartPane.setFocus(PartPane.java:325)
    at org.eclipse.ui.internal.EditorPane.setFocus(EditorPane.java:127)
    at org.eclipse.ui.internal.PartStack.presentationSelectionChanged(PartStack.java:844)
    at org.eclipse.ui.internal.PartStack.access$1(PartStack.java:827)
    at org.eclipse.ui.internal.PartStack$1.selectPart(PartStack.java:137)
    at org.eclipse.ui.internal.presentations.util.TabbedStackPresentation$1.handleEvent(TabbedStackPresentation.java:133)
    at org.eclipse.ui.internal.presentations.util.AbstractTabFolder.fireEvent(AbstractTabFolder.java:269)
    at org.eclipse.ui.internal.presentations.util.AbstractTabFolder.fireEvent(AbstractTabFolder.java:278)
    at org.eclipse.ui.internal.presentations.defaultpresentation.DefaultTabFolder.access$1(DefaultTabFolder.java:1)
    at org.eclipse.ui.internal.presentations.defaultpresentation.DefaultTabFolder$2.handleEvent(DefaultTabFolder.java:88)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:3783)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1375)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1398)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1383)
    at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1195)
    at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:2743)
    at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1429)
    at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:257)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:3783)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1375)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1398)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1383)
    at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1195)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3629)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3284)
    at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
    at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
    at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
    at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:620)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:575)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1408)
    

    What can i do? where does the error born? The elements under my custom view aren't displayed!

  • Jayyrus
    Jayyrus over 12 years
    yes, true, i made error when i write post, but in eclipse is all ok
  • HefferWolf
    HefferWolf over 12 years
    if movie is null, I guess "is" is also null, which is because "R.drawable.anim_cerca" cannot be opened as raw resource, as it is compressed in your apk. Try storing your gif either in "assets" or "raw".
  • Jayyrus
    Jayyrus over 12 years
    and how can i get it to create is?
  • Alan Moore
    Alan Moore over 12 years
    I suggest you update your post, it's hard to read the code when its not accurate.
  • HefferWolf
    HefferWolf over 12 years
    as I said: store your gif in the "assets" folder of your android project and access the gif using 'context.getAssets().open("your-file-name-here.gif");' which also returns an input stream
  • Jayyrus
    Jayyrus over 12 years
    the problem still remain... does eclipse compile, every time i open xml, all the .java associated with xml?
  • HefferWolf
    HefferWolf over 12 years
    to be sure: do a "clean project" on your android project ;). Always a good thing to do, if you changed your resources and something strange happens.
  • HefferWolf
    HefferWolf over 12 years
  • Alan Moore
    Alan Moore over 12 years
    looking at your log file, you need to look at the code is at line 27 as in com.digit.GIFView.<init>(GIFView.java:27) since that is where the exception is happening. I can't tell which line that is from what you've posted.
  • Jayyrus
    Jayyrus over 12 years
    scuse me but i wasn't next to pc, so, my problem is that gif is visualized in device, but all the elements under my custom views aren't displayed.. what can i do?
  • HefferWolf
    HefferWolf over 12 years
    after the NullPointerException the rest of your dialog doesn't get layouted. Again, try to move the gif to the assets folder.