2012-02-25 62 views
1

我正在學習android-development。當我使用Gallery時,有一個問題。這裏是代碼:android gallery

Gallery cf=new Gallery(this); 
cf.setSpacing(20); 
cf.setAdapter(imageAdapter); 
cf.setAnimationDuration(1500); 
setContentView(cf); 

但是,當我嘗試,這是錯誤的。

Gallery cf=(Gallery)this.findViewById(R.id.Gallery); 
cf.setSpacing(20); 
cf.setAdapter(imageAdapter); 
cf.setAnimationDuration(1500); 
setContentView(R.layout.display); 

<Gallery 
android:id="@+id/Gallery" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_centerInParent="true" 
android:spacing="-60px"/ 
> 

我該怎麼辦?

編輯: logcat的:

02-25 15:22:45.009: E/AndroidRuntime(500): FATAL EXCEPTION: main 
02-25 15:22:45.009: E/AndroidRuntime(500): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vic.android.BluetoothContacts/com.vic.android.BluetoothContact‌​s.PhotoDisplay}: java.lang.NullPointerException 
02-25 15:22:45.009: E/AndroidRuntime(500): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
02-25 15:22:45.009: E/AndroidRuntime(500): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
+1

到底是什麼問題? – 2012-02-25 15:18:12

+0

給我們Log Cat的錯誤信息和stacktrace我們不是巫術的人,可以通過你的頭腦或屏幕看到。 – AsTeR 2012-02-25 15:24:57

+0

Thanks.02-25 15:22:45.009:E/AndroidRuntime(500):致命例外:main 02-25 15:22:45.009:E/AndroidRuntime(500):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.vic.android.BluetoothContacts/com.vic.android.BluetoothContacts.PhotoDisplay}:java.lang.NullPointerException 02:15:22:45.009:E/AndroidRuntime(500):\t at android.app.ActivityThread .performLaunchActivity(ActivityThread.java:1647) 02-25 15:22:45.009:E/AndroidRuntime(500):\t at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) – Vic 2012-02-25 15:28:27

回答

1

您是的setContentView findById後 - 我認爲這就是問題所在 嘗試

setContentView(R.layout.display); 
Gallery cf=(Gallery)this.findViewById(R.id.Gallery); 
cf.setSpacing(20); 
cf.setAdapter(imageAdapter); 
cf.setAnimationDuration(1500); 
+0

Yes.And我已經解決了它。謝謝。 – Vic 2012-03-07 03:11:08