我有一个接一个地以编程方式填充Gallery组件的LinearLayout areaForGalleries.每个库中的onItemClick方法将其从areaForGalleries中删除,并将其添加到RelativeLayout parentLayout,它是areaForGalleries的父级.见下面的代码.
LinearLayout areaForGalleries;
RelativeLayout parentLayout;
...
private void showGallery() {
final CustomGallery mGallery = new CustomGallery(mContext);
mGallery.setOnItemClickListener(new CustomAdapterView.OnItemClickListener() {
public void onItemClick(CustomAdapterView<?> parent, View v, int position, long id) {
areaForGalleries.removeView(mGallery);
parentLayout.addView(mGallery);
}
});
CustomGallery是我对Android Gallery组件的实现.我只更改了左侧图库的对齐方式 – 一行代码已更改.
代码在Android 2.1 – 2.3.3上完美运行,但是当在Honeycomb或ICS上尝试时,它崩溃并出现以下错误:
01-31 10:31:49.596: E/AndroidRuntime(1536): FATAL EXCEPTION: main
01-31 10:31:49.596: E/AndroidRuntime(1536): java.lang.NullPointerException
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:587)
01-31 10:31:49.596: E/AndroidRuntime(1536): at com.my.example.pullrefreshgallery.CustomGallery.onTouchEvent(CustomGallery.java:812)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.View.dispatchTouchEvent(View.java:5486)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1953)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1714)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
01-31 10:31:49.596: E/AndroidRuntime(1536): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1892)
01-31 10:31:49.596: E/AndroidRuntime(1536): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
01-31 10:31:49.596: E/AndroidRuntime(1536): at com.my.example.Home.dispatchTouchEvent(Home.java:883)
01-31 10:31:49.596: E/AndroidRuntime(1536): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1840)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.View.dispatchPointerEvent(View.java:5662)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2863)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.os.Looper.loop(Looper.java:137)
01-31 10:31:49.596: E/AndroidRuntime(1536): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-31 10:31:49.596: E/AndroidRuntime(1536): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 10:31:49.596: E/AndroidRuntime(1536): at java.lang.reflect.Method.invoke(Method.java:511)
01-31 10:31:49.596: E/AndroidRuntime(1536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-31 10:31:49.596: E/AndroidRuntime(1536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-31 10:31:49.596: E/AndroidRuntime(1536): at dalvik.system.NativeStart.main(Native Method)
NullPointerException是指CustomGallery方法,我根本没有改变:
@Override
public boolean onTouchEvent(MotionEvent event) {
// Give everything to the gesture detector
boolean retValue = mGestureDetector.onTouchEvent(event);
int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {
// Helper method for lifted finger
onUp();
} else if (action == MotionEvent.ACTION_CANCEL) {
onCancel();
}
return retValue;
}
当我评论删除和添加mGallery的行时,一切都很完美!
有similar problem,但我没有使用碎片.
有什么想法有什么不对?
谢谢!
解决方法:
我现在不是你等待的答案,但我只是在原始Gallery小部件上的ICS上遇到了同样的问题(没有超载).
经过一些测试后,我最终使用了简单的方法:尝试…捕获
@Override
public boolean onTouchEvent(MotionEvent event) {
try {
return super.onTouchEvent(event);
} catch (Throwable t) {
return true;
}
}
厨房仍然正常工作,onClickListener也是如此,所以对我没有任何副作用.