我正在使用图书馆" DraggableGridView"对于Android,我试图让一些图像能够改变它们之间的位置,但应用程序一直在崩溃,我在这里留下一些代码,希望有人可以帮助我:S
public class HomeFragment extends Fragment {
private ImageView one;
private ImageView two;
private DraggableGridView dgv;
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
dgv = (DraggableGridView)rootView.findViewById(R.id.dgv);
one =(ImageView)rootView.findViewById(R.id.moodleLogo);
two =(ImageView)rootView.findViewById(R.id.aulawebLogo);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
setListeners();
}
private void setListeners() {
dgv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
String url = (String)dgv.getChildAt(arg2).getTag();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
logcat说:
04-24 23:46:53.860: E/InputEventReceiver(20805): Exception dispatching input event.
04-24 23:46:53.890: E/AndroidRuntime(20805): FATAL EXCEPTION: main
04-24 23:46:53.890: E/AndroidRuntime(20805): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-24 23:46:53.890: E/AndroidRuntime(20805): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
04-24 23:46:53.890: E/AndroidRuntime(20805): at java.util.ArrayList.get(ArrayList.java:304)
04-24 23:46:53.890: E/AndroidRuntime(20805): at com.animoto.android.views.DraggableGridView.animateGap(DraggableGridView.java:331)
04-24 23:46:53.890: E/AndroidRuntime(20805): at com.animoto.android.views.DraggableGridView.onTouch(DraggableGridView.java:253)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.View.dispatchTouchEvent(View.java:7345)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2412)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2147)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2418)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2161)
04-24 23:46:53.890: E/AndroidRuntime(20805): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2139)
04-24 23:46:53.890: E/AndroidRuntime(20805): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1476)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.app.Activity.dispatchTouchEvent(Activity.java:2487)
04-24 23:46:53.890: E/AndroidRuntime(20805): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2087)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.View.dispatchPointerEvent(View.java:7535)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3415)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3347)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4456)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4434)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4538)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4506)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4557)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.Choreographer.doFrame(Choreographer.java:523)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.os.Handler.handleCallback(Handler.java:615)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.os.Handler.dispatchMessage(Handler.java:92)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.os.Looper.loop(Looper.java:137)
04-24 23:46:53.890: E/AndroidRuntime(20805): at android.app.ActivityThread.main(ActivityThread.java:4921)
04-24 23:46:53.890: E/AndroidRuntime(20805): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 23:46:53.890: E/AndroidRuntime(20805): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 23:46:53.890: E/AndroidRuntime(20805): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
04-24 23:46:53.890: E/AndroidRuntime(20805): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
04-24 23:46:53.890: E/AndroidRuntime(20805): at dalvik.system.NativeStart.main(Native Method)
04-24 23:46:53.940: E/android.os.Debug(3368): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error
提前致谢
答案 0 :(得分:0)
不要使用
String url = (String)dgv.getChildAt(arg2).getTag();
getChildAt
方法采用与位置(arg2
)无关的int。
相反,通过查询适配器找到您的URL。
假设您的适配器包含String
s:
String url = arg0.getAdapter().getItem(arg2);