我自己正在学习 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"/
>
我该怎么办?
编辑: 日志:
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.BluetoothContacts.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)
最佳答案
你是 findById 之后的 setContentView - 我认为这就是问题所在 尝试
setContentView(R.layout.display);
Gallery cf=(Gallery)this.findViewById(R.id.Gallery);
cf.setSpacing(20);
cf.setAdapter(imageAdapter);
cf.setAnimationDuration(1500);
关于安卓图库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9445183/