У меня LinearLayout areaForGalleries
программно заполнены Gallery
компонентами, один за другим.Метод 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
, все работает отлично!Была похожая проблема , но я не использую фрагменты.
Есть идеи, что не так?Спасибо!