我刚刚开始使用Android,并认为创建一个新的图像类很容易,我将在后面移动等等,只是基本上玩Android。好吧,它似乎并不像预期的那么容易。我得到一份NPE:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context=".Start">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_gravity="center" />
</FrameLayout>
package com.andreiv.snake;
import com.andreiv.snake.util.Picture;
import com.andreiv.snake.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class Start extends Activity {
ImageView img;
Picture pic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
img = (ImageView)findViewById(R.id.imageView);
pic = new Picture(img.getContext());
pic.drawPic();
setContentView(R.layout.activity_start);
}
}
package com.andreiv.snake.util;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import com.andreiv.snake.R;
/**
* Created by andrei on 7/2/2014.
*/
public class Picture extends ImageView{
public Picture(Context context) {
super(context);
}
public Picture(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Picture(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void drawPic() {
setImageResource(R.drawable.pic);
}
}
答案 0 :(得分:3)
首先 - 您需要在尝试使用FindViewById之前设置内容视图。
第二 - 你不需要从图像中获取上下文,你可以给活动本身(活动扩展上下文),在你的情况下即使这是没有必要的,因为视图将在框架时自动获取上下文会膨胀你的xml。
第三 - 如果您希望扩展视图在xml中膨胀,则必须将其类名放在xml(而不是ImageView类)中
public class Start extends Activity {
Picture img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
img = (Picture)findViewById(R.id.imageView);
img.drawPic();
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context=".Start">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<com.andreiv.snake.util.Picture
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_gravity="center" />