所以当我编码的时候,我遇到了一些逻辑上无法解释的东西。
public class Player {
ImageView playerImage;
Bitmap playerBmp;
InputStream is;
Drawable drawable;
public void draw(){
PlayActivity activity = new PlayActivity();
playerImage = (ImageView) activity.findViewById(R.id.playerImage);
is = activity.getResources().openRawResource(+R.drawable.main_character_drawing);
playerBmp = BitmapFactory.decodeStream(is);
drawable = new BitmapDrawable(activity.getResources(), playerBmp);
playerImage.setImageDrawable(drawable);
}
}
这是PlayActivity类
public class PlayActivity extends Activity {
Player player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
player = new Player();
player.draw();
}
}
这是activity_play的XML
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/playerImage"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="100dp"
android:layout_marginLeft="250dp"/>
但是,当我运行player.draw()时,我得到一个空指针异常。 这是错误日志:
07-07 17:49:05.291 31892-31892/com.example.testGame.henry. E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testGame.henry./com.example.testGame.henry.PlayActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
at android.app.ActivityThread.access$600(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4849)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:2045)
at com.example.testGame.henry.Player.draw(Player.java:36)
at com.example.testGame.henry.PlayActivity.onCreate(PlayActivity.java:26)
at android.app.Activity.performCreate(Activity.java:5244)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
at android.app.ActivityThread.access$600(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4849)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
长期做空,问题似乎是这行代码:
playerImage = (ImageView) activity.findViewById(R.id.playerImage);
是否有某种原因会导致空指针异常?
我还发现,如果我更换了
使用draw()
的{{1}}方法,然后删除该行
draw(Activity activity)
而只是简单地写了PlayActivity activity = new PlayActivity();
(player.draw(this)
是PlayActivity),它会运行得很好。
没有空指针异常,即使我找到的解决方案与创建PlayActivity实例完全相同。造成这种情况的原因是什么?
我不明白为什么在创建和使用PlayActivity实例时它会给出一个空指针异常,但是当我将PlayActivity传递给一个方法并使用参数变量(即PlayActivity)时,它就不会出现这种情况。 / p>
答案 0 :(得分:1)
PlayActivity activity = new PlayActivity();
不会为您提供现有的PlayActivity实例。它给你一些东西,如果有的话,没有有效的上下文。为了使其工作,您需要使用适当的PlayActivity实例来查找视图并对其进行充气。或多或少就像你在文中所描述的那样。
答案 1 :(得分:0)
原因是您正在调用Activity的新实例。它没有传播的上下文。看看这是否有效。
public class PlayActivity extends Activity {
Player player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
player = new Player();
player.draw(this); <-- THIS
}
}
public class Player {
ImageView playerImage;
Bitmap playerBmp;
InputStream is;
Drawable drawable;
public void draw(Activity activity){ <-- THIS
playerImage = (ImageView) activity.findViewById(R.id.playerImage);
is = activity.getResources().openRawResource(+R.drawable.main_character_drawing);
playerBmp = BitmapFactory.decodeStream(is);
drawable = new BitmapDrawable(activity.getResources(), playerBmp);
playerImage.setImageDrawable(drawable);
}
}