Allright som首先对我的Android编程非常新,并试图做一些非常简单和基本的事情。包含各种图片的程序,具体取决于您单击其显示的按钮。我没有为我想要设置为背景的每张图片创建一个新的xml文件,而是考虑制作一个xml文件,然后调用它来更改背景。所以我试图使用的是:
public class Picture extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=getIntent();
int picture=intent.getIntExtra("picture", 22);
ImageView image = (ImageView) findViewById(R.id.pansarvagn);
image.setBackgroundResource(picture);
setContentView(R.layout.tmp2);
}
}
然后我做了这个:
public void displayPicture(int pictureresource){
Intent intent = new Intent();
intent.putExtra("picture", pictureresource);
intent.setClass(getApplicationContext(), Picture.class);
startActivity(intent);
}
最后一个按钮调用如下所示:
ImageView image = (ImageView) findViewById(R.id.pansarvagn);
Button spgsu8 = (Button) findViewById(R.id.su8);
spgsu8.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
image.setBackgroundResource(R.drawable.su8);
displayPicture(R.drawable.su8);
然后这是在我的xml文件中,或者我尝试了一堆东西,但我想调用id并更改图片/背景:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/su8" android:id="@+id/pansarvagn"></ImageView>
但无论我怎么扭曲它,我仍然想出来:
08-30 06:47:07.568: ERROR/AndroidRuntime(449): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wot/com.example.wot.Picture}: java.lang.NullPointerException
08-30 06:47:07.568: ERROR/AndroidRuntime(449): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-30 06:47:07.568: ERROR/AndroidRuntime(449): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-30 06:47:07.568: ERROR/AndroidRuntime(449): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-30 06:47:07.568: ERROR/AndroidRuntime(449): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-30 06:47:07.568: ERROR/AndroidRuntime(449): at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 06:47:07.568: ERROR/AndroidRuntime(449): at android.os.Looper.loop(Looper.java:123)
08-30 06:47:07.568: ERROR/AndroidRuntime(449): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-30 06:47:07.568: ERROR/AndroidRuntime(449): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 06:47:07.568: ERROR/AndroidRuntime(449): at java.lang.reflect.Method.invoke(Method.java:507)
08-30 06:47:07.568: ERROR/AndroidRuntime(449): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-30 06:47:07.568: ERROR/AndroidRuntime(449): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-30 06:47:07.568: ERROR/AndroidRuntime(449): at dalvik.system.NativeStart.main(Native Method)
08-30 06:47:07.568: ERROR/AndroidRuntime(449): Caused by: java.lang.NullPointerException
08-30 06:47:07.568: ERROR/AndroidRuntime(449): at com.example.wot.Picture.onCreate(Picture.java:17)
08-30 06:47:07.568: ERROR/AndroidRuntime(449): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-30 06:47:07.568: ERROR/AndroidRuntime(449): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-30 06:47:07.568: ERROR/AndroidRuntime(449): ... 11 more
我希望你们能够做到这一点:&gt;
答案 0 :(得分:0)
尝试以下代码...我们无法在view
setcontentview
public class Picture extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=getIntent();
int picture=intent.getIntExtra("picture", 22);
setContentView(R.layout.tmp2);
ImageView image = (ImageView) findViewById(R.id.pansarvagn);
image.setBackgroundResource(picture);
}
}