SwipeRefreshLayout导致NullPointerException异常中的onCreate [英] SwipeRefreshLayout causes NullPointerException in onCreate
问题描述
运行我的SwipeRefreshLayout应用导致应用程序崩溃。我试图调试的问题,但似乎在 setColorScheme
在它进入 ensureLayout
发生p>
在logcat的:
16 07 - 09:46:56.093:E / AndroidRuntime(18659):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.heath_bar.twitter / com.heath_bar。 twitter.MainActivity}:显示java.lang.NullPointerException
07-09 16:46:56.093:E / AndroidRuntime(18659):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
07-09 16:46:56.093:E / AndroidRuntime(18659):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2386)
07-09 16:46:56.093:E / AndroidRuntime(18659):在android.app.ActivityThread.access $ 900(ActivityThread.java:169)
07-09 16:46:56.093:E / AndroidRuntime(18659):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1277)
07-09 16:46:56.093:E / AndroidRuntime(18659):在android.os.Handler.dispatchMessage(Handler.java:102)
07-09 16:46:56.093:E / AndroidRuntime(18659):在android.os.Looper.loop(Looper.java:136)
07-09 16:46:56.093:E / AndroidRuntime(18659):在android.app.ActivityThread.main(ActivityThread.java:5476)
07-09 16:46:56.093:E / AndroidRuntime(18659):在java.lang.reflect.Method.invokeNative(本机方法)
07-09 16:46:56.093:E / AndroidRuntime(18659):在java.lang.reflect.Method.invoke(Method.java:515)
07-09 16:46:56.093:E / AndroidRuntime(18659):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1268)
07-09 16:46:56.093:E / AndroidRuntime(18659):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
07-09 16:46:56.093:E / AndroidRuntime(18659):在dalvik.system.NativeStart.main(本机方法)
07-09 16:46:56.093:E / AndroidRuntime(18659):致:显示java.lang.NullPointerException
07-09 16:46:56.093:E / AndroidRuntime(18659):在android.support.v4.widget.SwipeRefreshLayout.ensureTarget(SwipeRefreshLayout.java:293)
07-09 16:46:56.093:E / AndroidRuntime(18659):在android.support.v4.widget.SwipeRefreshLayout.setColorScheme(SwipeRefreshLayout.java:268)
07-09 16:46:56.093:E / AndroidRuntime(18659):在com.heath_bar.twitter.MainActivity.onCreate(MainActivity.java:56)
07-09 16:46:56.093:E / AndroidRuntime(18659):在android.app.Activity.performCreate(Activity.java:5451)
07-09 16:46:56.093:E / AndroidRuntime(18659):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
07-09 16:46:56.093:E / AndroidRuntime(18659):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
07-09 16:46:56.093:E / AndroidRuntime(18659):... 11个
下面是我的code:
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
SwipeRefreshLayout swipeLayout =(SwipeRefreshLayout)findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(新OnRefreshListener(){ @覆盖
公共无效onRefresh(){
// TODO自动生成方法存根 }
});
swipeLayout.setColorScheme(Color.RED,Color.BLUE,Color.GREEN,
Color.YELLOW);}
我的XAML:
<?XML版本=1.0编码=UTF-8&GT?;
< android.support.v4.widget.SwipeRefreshLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / swipe_container
机器人:layout_width =match_parent
机器人:layout_height =match_parent><滚动型
机器人:ID =@ + ID /滚动视图
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT> < TableLayout
机器人:ID =@ + ID /表
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_margin =5DP/>
< /滚动型>
< /android.support.v4.widget.SwipeRefreshLayout>
任何想法,为什么这可能是崩溃?我加入了正确的jar文件,以及到我的Android下的私家藏书的项目。
所有的想法都大大AP preciated。谢谢你在前进。
不是知道在哪里的问题是,但我相信我有android.support.v4版本20.0不19.1,但百分之百的一些奇怪的原因, setColorScheme
没有标记为pcated德$ p $(因为它是在20.0)。删除库,并重新添加android.support.v4库版本20.0。然后更改 setColorScheme
设置 setColorSchemeColors
固定的问题。
我希望这样可以节省别人一些时间。
Running my app with SwipeRefreshLayout is causing the app to crash. I have tried to debug the issue, but it seems to occur after the setColorScheme
in which it goes into ensureLayout
The logcat:
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
Here is my code:
@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);
}
My xaml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="@+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableLayout
android:id="@+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
Any ideas why this might be crashing? I have added the correct jar file as well into my project under Android Private Libraries.
All ideas are greatly appreciated. Thank you in advance.
Not a hundred percent sure where the problem was, but I believe I had android.support.v4 version 20.0 not 19.1, but for some odd reason, setColorScheme
was not marked as deprecated (as it is in 20.0). Deleting the library and re-adding android.support.v4 library version 20.0. Then changing setColorScheme
to set setColorSchemeColors
fixed the issue.
I hope this can save someone else some time.
这篇关于SwipeRefreshLayout导致NullPointerException异常中的onCreate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!