user1044445
user1044445

Reputation: 21

ImageView findViewById and setContent(R.layout.main) doesn't work

I have a problem with this simple code. I'm trying everything possible, but setContent(R.layout.main) always causes an error. If I tried debug code svgView = (SVGView)findViewById(R.id.svgview) value was always null.

This is my code:

SVGViewActivity.java :

    package android.svgview;

    import com.larvalabs.svgandroid.SVG;
    import com.larvalabs.svgandroid.SVGParser;

    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;

    public class SVGViewActivity extends Activity {
        /** Called when the activity is first created. */

    private SVGView svgView;
    private ViewListener viewListener;
    private ViewState viewState;
    private SVG svg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        svgView = (SVGView)findViewById(R.id.svgview);
        viewListener = new ViewListener();
        viewState = new ViewState();

        svgView.setViewState(viewState);
        svgView.setOnTouchListener(viewListener);
        viewListener.setViewState(viewState);

        svg = SVGParser.getSVGFromResource(getResources(), R.drawable.android);
        svgView.setImageDrawable(svg.createPictureDrawable());
        svgView.setBackgroundColor(Color.WHITE);

    }
}

SVGView.java

package android.svgview;

import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.graphics.Canvas;
import android.widget.ImageView;

public class SVGView extends ImageView implements Observer {

    private ViewState viewState;

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

    public void setViewState(ViewState viewState){
        if(this.viewState==null){
            this.viewState = viewState;
            this.viewState.addObserver(this);
        }
    }

    protected void onDraw(Canvas canvas){

        canvas.scale(1.0f, 1.0f);
        canvas.translate(viewState.getX(), viewState.getY());

        super.onDraw(canvas);

    }
    public void update(Observable observable, Object data) {
        invalidate();

    }
}

and main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.svgview.SVGView
        android:id="@+id/svgview" 
        />
</LinearLayout>

When I change svgView = (SVGView)findViewById(R.id.svgview); for svgView = new SVGView(this); and then setContentView(R.layout.main); for setContentView(svgView); everything works fine. But using main.xml causes errors.

I tried clean and recompile project, compile it in new eclipse instalation with all updates but no effect. I have no idea where is a problem :-/

Upvotes: 1

Views: 1674

Answers (2)

Shailendra Singh Rajawat
Shailendra Singh Rajawat

Reputation: 8242

add constructor

public SVGView(Context context, AttribeSet attributeSer)
 {
        super(context, attributeSet);
    }

Upvotes: 0

kaspermoerch
kaspermoerch

Reputation: 16570

You probably need to supply this contructor in your SVGView.java:

public SVGView(Context context AttributeSet atrs ) {
    super(context, atrs);
}

Then change the declaration in your xml-file to this:

<android.svgview.SVGView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/svgview" /> 

Upvotes: 8

Related Questions