好的,这非常令人沮丧。
我的代码如下:
accept.setBackgroundResource(R.drawable.circle_navigator_accept_checked);
ViewPager vp = (ViewPager)activity.findViewById(R.id.viewpager);
ViewPagerAdapter adapter = (ViewPagerAdapter)vp.getAdapter();
MyAdapter ma = (MyAdapter)(adapter.finalExamTypeList.getAdapter());
int selected = ma.getSelectedItem();
selected -= adapter.finalExamTypeList.getFirstVisiblePosition();
我在这一行得到NullPointerException
:
MyAdapter ma = (MyAdapter)(adapter.finalExamTypeList.getAdapter());
似乎很容易找到 - adapter
或adapter.finalExamTypeList
应为null
。
但是当我在调试模式下检查它时,此行中的任何内容都不是null
。一切都已初始化,并且已正确初始化。此adapter.finalExamTypeList.getAdapter()
也会正确返回MyAdapter
个对象。
所以我有点担心,但无论如何,我试图在这里制定这些条件:
accept.setBackgroundResource(R.drawable.circle_navigator_accept_checked);
ViewPager vp = (ViewPager)activity.findViewById(R.id.viewpager);
ViewPagerAdapter adapter = (ViewPagerAdapter)vp.getAdapter();
if(adapter != null) {
if(adapter.finalExamTypeList != null) {
if(adapter.finalExamTypeList.getAdapter() != null) {
MyAdapter ma = (MyAdapter)(adapter.finalExamTypeList.getAdapter());
int selected = ma.getSelectedItem();
selected -= adapter.finalExamTypeList.getFirstVisiblePosition();
}
}
}
但是,NullPointerException
在同一个地方。
日志:
06-23 09:25:28.260: W/dalvikvm(1967): JNI WARNING: JNI method called with exception raised
06-23 09:25:28.260: W/dalvikvm(1967): in Landroid/os/Process;.setArgV0 (Ljava/lang/String;)V (GetStringCritical)
06-23 09:25:28.260: W/dalvikvm(1967): Pending exception is:
06-23 09:25:28.260: I/dalvikvm(1967): Ljava/lang/reflect/InvocationTargetException;:
06-23 09:25:28.260: I/dalvikvm(1967): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 09:25:28.260: I/dalvikvm(1967): at java.lang.reflect.Method.invoke(Method.java:507)
06-23 09:25:28.260: I/dalvikvm(1967): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-23 09:25:28.260: I/dalvikvm(1967): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-23 09:25:28.260: I/dalvikvm(1967): at dalvik.system.NativeStart.main(Native Method)
06-23 09:25:28.260: I/dalvikvm(1967): Caused by:
06-23 09:25:28.260: I/dalvikvm(1967): Ljava/lang/NullPointerException;:
06-23 09:25:28.260: I/dalvikvm(1967): at pl.lodz.uni.myproject.CircleNavigator$1.onClick(CircleNavigator.java:99)
06-23 09:25:28.260: I/dalvikvm(1967): at android.view.View.performClick(View.java:2485)
06-23 09:25:28.260: I/dalvikvm(1967): at android.view.View$PerformClick.run(View.java:9080)
06-23 09:25:28.260: I/dalvikvm(1967): at android.os.Handler.handleCallback(Handler.java:587)
06-23 09:25:28.260: I/dalvikvm(1967): at android.os.Handler.dispatchMessage(Handler.java:92)
06-23 09:25:28.260: I/dalvikvm(1967): at android.os.Looper.loop(Looper.java:130)
06-23 09:25:28.260: I/dalvikvm(1967): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-23 09:25:28.260: I/dalvikvm(1967): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 09:25:28.260: I/dalvikvm(1967): at java.lang.reflect.Method.invoke(Method.java:507)
06-23 09:25:28.260: I/dalvikvm(1967): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-23 09:25:28.260: I/dalvikvm(1967): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-23 09:25:28.260: I/dalvikvm(1967): at dalvik.system.NativeStart.main(Native Method)
06-23 09:25:28.260: I/dalvikvm(1967): "main" prio=5 tid=1 NATIVE
06-23 09:25:28.260: I/dalvikvm(1967): | group="main" sCount=0 dsCount=0 obj=0xb5f1bc38 self=0x9240e48
06-23 09:25:28.260: I/dalvikvm(1967): | sysTid=1967 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=-2145907712
06-23 09:25:28.260: I/dalvikvm(1967): at android.os.Process.setArgV0(Native Method)
06-23 09:25:28.260: I/dalvikvm(1967): at android.app.ActivityThread.main(ActivityThread.java:3668)
06-23 09:25:28.260: I/dalvikvm(1967): at java.lang.reflect.Method.invokeNative(Native Method)
06-23 09:25:28.260: I/dalvikvm(1967): at java.lang.reflect.Method.invoke(Method.java:507)
06-23 09:25:28.260: I/dalvikvm(1967): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-23 09:25:28.260: I/dalvikvm(1967): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-23 09:25:28.260: I/dalvikvm(1967): at dalvik.system.NativeStart.main(Native Method)
06-23 09:25:28.260: E/dalvikvm(1967): VM aborting
任何人都知道这里发生了什么?
答案 0 :(得分:4)
但是当我在调试模式下检查它时,此行中的任何内容都不为空。
有几种可能性你没有考虑过。
这可能是您在调试器中未观察到的竞争条件,因为单步执行代码时不会发生这种情况。
调试器中可能存在错误。
答案 1 :(得分:1)
我设法解决了这个问题。它似乎是某种错误或某种东西。在我重新启动eclipse几次后,它告诉我更新我的ADT,所以我做了,它解决了这个问题,没有NullPointerException
。魔法。
答案 2 :(得分:0)
ViewPager.getAdapter仅返回适配器。除非您在其他地方初始化它,否则您应该创建一个新的ViewPagerAdapter
。此外,您似乎也没有调用ViewPager.setAdapter,这在调用getAdapter
之前是必要的。