2014-11-06 2 views
0

Я пытаюсь установить цвета в SwipeRefreshLayout с использованием метода setColorSchemeColors, но он всегда бросает исключение NullPointerException, если я пытаюсь его использовать, что довольно изнашивается.SwipeRefreshLayout setColorSchemeColors всегда выбрасывает NullPointerException

Мой код деятельности выглядит следующим простым кодом:

swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.content); 
    swipeRefreshLayout.setOnRefreshListener(this); 
    swipeRefreshLayout.setColorSchemeColors(
      android.R.color.holo_blue_light, 
      android.R.color.holo_orange_light, 
      android.R.color.holo_green_light, 
      android.R.color.holo_red_light); 

Моя раскладка выглядит следующим образом:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/wrapper" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@color/background"> 

    <android.support.v4.widget.SwipeRefreshLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/content" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1"/> 

    <android.support.v4.app.FragmentTabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="0"/> 
    </android.support.v4.app.FragmentTabHost> 
</LinearLayout> 

Я попытался установить цвета даже в onResume (просто пытается) и он не работает ,

Исключение:

java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
      at android.app.ActivityThread.access$600(ActivityThread.java:141) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
      at android.os.Handler.dispatchMessage(Handler.java:99) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:5041) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at android.support.v4.widget.SwipeRefreshLayout.ensureTarget(SwipeRefreshLayout.java:312) 
      at android.support.v4.widget.SwipeRefreshLayout.setColorSchemeColors(SwipeRefreshLayout.java:292) 
      at .... 

Единственная проблема, которую я нашел здесь this, но все, кажется, не имеют каких-либо проблем.

Я что-то упустил?

+0

Просьба указать полный стек. –

+0

Я отправил важную часть, которая является методом android.support.v4.widget.SwipeRefreshLayout.ensureTarget – Proverbio

ответ

1

У SwipeRefreshLayout требуется наличие ребенка при попытке установить цвета анимации. Я добавил ребенка в макет и проблема исчезла.

<android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/swipeRefreshLayout" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1"> 

     <FrameLayout 
      android:id="@+id/content" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 


     </FrameLayout> 
</android.support.v4.widget.SwipeRefreshLayout> 
0

Обновлены только детские предметы android.support.v4.widget.SwipeRefreshLayout. У вас их нет. Я предполагаю, что код удаляет ваш SwipeRefreshLayout, потому что он пуст.

Смежные вопросы