简体   繁体   English

Android java.lang.RuntimeException

[英]Android java.lang.RuntimeException

Allright som first of im very new to android programming and was trying to do something pretty simple and basic. 好吧,Som首先对我来说是Android编程的新手,它试图做一些非常简单和基本的事情。 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. 我没有为我想设置为背景的每张图片创建一个新的xml文件,而是考虑只制作一个xml文件,然后调用它来更改背景。 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文件中,或者我尝试了一堆东西,但是我想调用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>

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 尝试下面的代码...我们不能在setcontentview之前定义任何view

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

 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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