2011-08-30 51 views
0

Allright som首先是我對android編程非常新穎的東西,並試圖做一些非常簡單和基本的事情。一個程序,其中包含各種圖片,並根據您點擊它顯示哪個按鈕。我沒有爲每張我想設置爲背景的圖片創建一個新的xml文件,而是考慮製作一個xml文件,然後調用它來更改背景。所以我試圖用是這樣的:Android java.lang.RuntimeException

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 

我希望你們能理解這SENCE:>

回答

1

試試下面的代碼......我們不能定義任何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); 

} 
} 
+0

非常感謝你!像一個魅力工作:D現在我終於可以繼續我的項目。非常感激! – Viktor

相關問題