简体   繁体   中英

Android java.lang.RuntimeException

Allright som first of im very new to android programming and was trying to do something pretty simple and basic. A program which contains various pictures, and depending on which button you click its showed. Instead of creating a new xml file for every picture I wanted to set as background I thought about just making one xml file, and then calling for it to change the background. so what I was trying to use was this:

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

and then ive made this:

public void displayPicture(int pictureresource){
Intent intent = new Intent();
intent.putExtra("picture", pictureresource);
intent.setClass(getApplicationContext(), Picture.class);
startActivity(intent);
}

and finally a button call looks like this:

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

and then this is in my xml file, or well i tried a bunch of stuff but i want to call on the id and change the pic/background:

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

but no matter how I twist it I still come up with:

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

I hope you guys can make sence of this :>

try below code ... we cant define any view before 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);

 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM