android画廊

时间:2012-02-25 15:11:22

标签: android gallery

我自己正在研究android-development。当我使用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 个答案:

答案 0 :(得分:1)

你是findById之后的setContentView - 我认为这是问题所在 尝试

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