SwipeRefreshLayout在onCreate中导致NullPointerException

时间:2014-07-09 23:56:30

标签: android swiperefreshlayout

使用SwipeRefreshLayout运行我的应用程序会导致应用程序崩溃。我试图调试此问题,但它似乎发生在进入setColorScheme的{​​{1}}之后

logcat:

ensureLayout

这是我的代码:

07-09 16:46:56.093: E/AndroidRuntime(18659): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.heath_bar.twitter/com.heath_bar.twitter.MainActivity}: java.lang.NullPointerException
07-09 16:46:56.093: E/AndroidRuntime(18659):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at android.app.ActivityThread.access$900(ActivityThread.java:169)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at android.os.Looper.loop(Looper.java:136)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at android.app.ActivityThread.main(ActivityThread.java:5476)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at java.lang.reflect.Method.invokeNative(Native Method)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at java.lang.reflect.Method.invoke(Method.java:515)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at dalvik.system.NativeStart.main(Native Method)
07-09 16:46:56.093: E/AndroidRuntime(18659): Caused by: java.lang.NullPointerException
07-09 16:46:56.093: E/AndroidRuntime(18659):    at android.support.v4.widget.SwipeRefreshLayout.ensureTarget(SwipeRefreshLayout.java:293)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at android.support.v4.widget.SwipeRefreshLayout.setColorScheme(SwipeRefreshLayout.java:268)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at com.heath_bar.twitter.MainActivity.onCreate(MainActivity.java:56)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at android.app.Activity.performCreate(Activity.java:5451)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
07-09 16:46:56.093: E/AndroidRuntime(18659):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
07-09 16:46:56.093: E/AndroidRuntime(18659):    ... 11 more

我的xaml:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 


    SwipeRefreshLayout swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    swipeLayout.setOnRefreshListener(new OnRefreshListener() {

        @Override
        public void onRefresh() {
            // TODO Auto-generated method stub

        }
    });
    swipeLayout.setColorScheme(Color.RED, Color.BLUE, Color.GREEN,
            Color.YELLOW);

}

为什么这可能会崩溃的任何想法?我已将正确的jar文件添加到Android私有库下的项目中。

enter image description here

非常感谢所有想法。提前谢谢。

2 个答案:

答案 0 :(得分:1)

不是百分之百确定问题在哪里,但我相信我有android.support.v4版本20.0而不是19.1,但由于一些奇怪的原因,setColorScheme未被标记为已弃用(因为它在20.0中) )。删除库并重新添加android.support.v4库版本20.0。然后将setColorScheme更改为设置setColorSchemeColors解决了问题。

我希望这可以节省一些时间。

答案 1 :(得分:1)

答案是你应该在swipeRefreshLayout添加了childView之后设置setColorScheme。试试吧!