Android 是否真的有必要通过超类传播上下文以从可绘制文件创建简单位图?
我有一个简单的问题,以前已经回答过很多次了,但是我不理解答案,我无法让我的代码正常工作 我想从可绘制的图形创建位图Android 是否真的有必要通过超类传播上下文以从可绘制文件创建简单位图?,android,android-drawable,android-context,Android,Android Drawable,Android Context,我有一个简单的问题,以前已经回答过很多次了,但是我不理解答案,我无法让我的代码正常工作 我想从可绘制的图形创建位图 public class Helicopter extends Sprite { private Context context; private Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot); public He
public class Helicopter extends Sprite {
private Context context;
private Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
public Helicopter(Context c) {
context = c;
}
}
public class TitleScreen extends State {
private Helicopter heli;
public TitleScreen(Context c) {
heli = new Helicopter(c);
}
}
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Game game = new Game(this, null);
game.pushState(new TitleScreen(game.getContext()));
setContentView(game);
}
}
我也在TitleScreen类中尝试了super.getGame().getContext()
,但两次尝试都在LogCat中给出了相同的错误:
01-26 13:28:32.217:E/AndroidRuntime(1537):
java.lang.RuntimeException:无法启动活动
ComponentInfo{com.example.helloandroid/com.example.helloandroid.MainActivity}:
java.lang.NullPointerException:尝试调用虚拟方法
'android.content.res.Resources android.content.Context.getResources()'
关于空对象引用
当我可以像这样访问drawable时,为什么创建位图如此困难代码>不引用上下文?
如何修复代码
谢谢中的上下文
private Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
未指定,因此无法引用资源。将初始化放在此处:
public Helicopter(Context c) {
context = c;
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
}
语境
private Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
未指定,因此无法引用资源。将初始化放在此处:
public Helicopter(Context c) {
context = c;
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
}
语境
private Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
未指定,因此无法引用资源。将初始化放在此处:
public Helicopter(Context c) {
context = c;
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
}
语境
private Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
未指定,因此无法引用资源。将初始化放在此处:
public Helicopter(Context c) {
context = c;
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
}
让我们考虑一下<代码>直升飞机< /代码>:
public class Helicopter extends Sprite {
private Context context;
private Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
public Helicopter(Context c) {
context = c;
}
}
首先,创建一个对象,将所有字段设置为0
或null
。然后,位图的初始值设定项工作:
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
但是存储到context
的值仍然是null
。你得到一个NPE
如果您没有NPE,那么之后将执行构造函数中的代码,将context
设置为c
听起来可能有悖直觉,但当代码在初始值设定项和静态初始值设定项中执行时,您可以在初始化字段中看到null
值。
例如:
class X {
int a = getB();
int b = 5;
int getB() { return b; }
}
public class A {
public static void main(String[] p) {
X x = new X();
System.out.println("x.a="+x.a+" x.b="+x.b);
}
}
运行和打印:
$ java A
x.a=0 x.b=5
让我们考虑一下<代码>直升飞机< /代码>:
public class Helicopter extends Sprite {
private Context context;
private Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
public Helicopter(Context c) {
context = c;
}
}
首先,创建一个对象,将所有字段设置为0
或null
。然后,位图的初始值设定项工作:
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
但是存储到context
的值仍然是null
。你得到一个NPE
如果您没有NPE,那么之后将执行构造函数中的代码,将context
设置为c
听起来可能有悖直觉,但当代码在初始值设定项和静态初始值设定项中执行时,您可以在初始化字段中看到null
值。
例如:
class X {
int a = getB();
int b = 5;
int getB() { return b; }
}
public class A {
public static void main(String[] p) {
X x = new X();
System.out.println("x.a="+x.a+" x.b="+x.b);
}
}
运行和打印:
$ java A
x.a=0 x.b=5
让我们考虑一下<代码>直升飞机< /代码>:
public class Helicopter extends Sprite {
private Context context;
private Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
public Helicopter(Context c) {
context = c;
}
}
首先,创建一个对象,将所有字段设置为0
或null
。然后,位图的初始值设定项工作:
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
但是存储到context
的值仍然是null
。你得到一个NPE
如果您没有NPE,那么之后将执行构造函数中的代码,将context
设置为c
听起来可能有悖直觉,但当代码在初始值设定项和静态初始值设定项中执行时,您可以在初始化字段中看到null
值。
例如:
class X {
int a = getB();
int b = 5;
int getB() { return b; }
}
public class A {
public static void main(String[] p) {
X x = new X();
System.out.println("x.a="+x.a+" x.b="+x.b);
}
}
运行和打印:
$ java A
x.a=0 x.b=5
让我们考虑一下<代码>直升飞机< /代码>:
public class Helicopter extends Sprite {
private Context context;
private Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
public Helicopter(Context c) {
context = c;
}
}
首先,创建一个对象,将所有字段设置为0
或null
。然后,位图的初始值设定项工作:
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heli1_east_rot);
但是存储到context
的值仍然是null
。你得到一个NPE
如果您没有NPE,那么之后将执行构造函数中的代码,将context
设置为c
听起来可能有悖直觉,但当代码在初始值设定项和静态初始值设定项中执行时,您可以在初始化字段中看到null
值。
例如:
class X {
int a = getB();
int b = 5;
int getB() { return b; }
}
public class A {
public static void main(String[] p) {
X x = new X();
System.out.println("x.a="+x.a+" x.b="+x.b);
}
}
运行和打印:
$ java A
x.a=0 x.b=5
明显地解决了的。谢谢,很明显。解决了的。谢谢,很明显。解决了的。谢谢,很明显。解决了的。谢谢