android - ImageView findViewById 和 setContent(R.layout.main) 不起作用

标签 android imageview nullpointerexception

我对这段简单的代码有疑问。我正在尝试所有可能的方法,但 setContent(R.layout.main) 总是会导致错误。如果我尝试调试代码 svgView = (SVGView)findViewById(R.id.svgview) 值始终为空。

这是我的代码:

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();

    }
}

和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>

当我将 svgView = (SVGView)findViewById(R.id.svgview); 更改为 svgView = new SVGView(this); 然后 setContentView( R.layout.main); 对于 setContentView(svgView); 一切正常。但是使用 main.xml 会导致错误。

我尝试清理并重新编译项目,在新的 Eclipse 安装中编译它并进行所有更新但没有效果。我不知道哪里出了问题:-/

最佳答案

您可能需要在 SVGView.java 中提供此构造器:

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

然后将 xml 文件中的声明更改为:

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

关于android - ImageView findViewById 和 setContent(R.layout.main) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8182604/

相关文章:

java - 如何通过在android中的.java文件中提供imgsrc来加载imageview

android - ActionBar Sherlock SwitchWidget

android - 如何在android中的同一底线对齐两个 ImageView ?

android - flutter 和 Firebase : Execution failed for task ':firebase_auth:compileDebugJavaWithJavac'

ios - iOS中的图像裁剪问题

java - 处理异常而不导致套接字服务器崩溃?

java - 什么是NullPointerException,我该如何解决?

java - getClassLoader().getResource(filepath) 返回空指针

android - 无法在opencv中加载haarcascadeshaarcascade.xml

android - Android Studio更新后,Gradle文件未下载