web-dev-qa-db-fra.com

Swipe Refresh Layout in Android Fragment

Im essayant d'implémenter SwipeRefreshLayout sur un fragment.

Je l'ai déjà résolu pour l'implémenter sur une activité, mais quand je l'implémente sur Fragment: Java.lang.NoClassDefFoundError: com ...

Est-ce que quelqu'un a une idée? Merci!

Article similaire sans réponses/solution: Swipe Refresh Layout - Class not found

Mon code:

public class Principal extends Fragment implements OnRefreshListener {
    SwipeRefreshLayout swipeLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.principal, container, false);

        swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorScheme(Android.R.color.holo_blue_bright, 
                Android.R.color.holo_green_light, 
                Android.R.color.holo_orange_light, 
                Android.R.color.holo_red_light);

        return view;
    }
}

Et ma mise en page:

<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">

    <com.enhancedListView.EnhancedListView
        Android:id="@+id/listaNoticias"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:layout_marginLeft="6dp"
        Android:layout_marginRight="6dp"
        Android:layout_marginTop="5dp" >
    </com.enhancedListView.EnhancedListView>

</Android.support.v4.widget.SwipeRefreshLayout>

Trace de pile complète

08-13 11:36:19.292: E/AndroidRuntime(31572): FATAL EXCEPTION: main
08-13 11:36:19.292: E/AndroidRuntime(31572): Process: com.xxx.xx, PID: 31572
08-13 11:36:19.292: E/AndroidRuntime(31572): Java.lang.NoClassDefFoundError: com.xxx.fragments.Principal
08-13 11:36:19.292: E/AndroidRuntime(31572):    at com.xxx.hem.activities.MainActivity.CargarPrincipal(MainActivity.Java:676)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at com.xxx.hem.activities.MainActivity.onCreate(MainActivity.Java:297)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at Android.app.Activity.performCreate(Activity.Java:5231)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1087)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2148)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2233)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at Android.app.ActivityThread.access$800(ActivityThread.Java:135)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1196)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at Android.os.Handler.dispatchMessage(Handler.Java:102)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at Android.os.Looper.loop(Looper.Java:136)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at Android.app.ActivityThread.main(ActivityThread.Java:5001)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at Java.lang.reflect.Method.invoke(Native Method)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:785)
08-13 11:36:19.292: E/AndroidRuntime(31572):    at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:601)
13
Nacho

Résolu :)

Mon XML

<?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" >

    <com.enhancedListView.EnhancedListView
        Android:id="@+id/listaNoticias"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:layout_marginLeft="6dp"
        Android:layout_marginRight="6dp"
        Android:layout_marginTop="5dp" >
    </com.enhancedListView.EnhancedListView>

</Android.support.v4.widget.SwipeRefreshLayout>

Mon fragment

public class Principal extends Fragment implements OnRefreshListener {
SwipeRefreshLayout swipeLayout;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.principal, container, false);

    swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
    swipeLayout.setOnRefreshListener(this);
    swipeLayout.setColorSchemeColors(Android.R.color.holo_green_dark, 
            Android.R.color.holo_red_dark, 
            Android.R.color.holo_blue_dark, 
            Android.R.color.holo_orange_dark); (...)

@Override
public void onRefresh() {
    new myTask().execute();     
}
21
Nacho

Mettez votre initialisation swipeLayout dans onViewCreated (vue View, bundle Bundle) au lieu de onCreateView (gonfleur LayoutInflater, conteneur ViewGroup, Bundle savedInstanceState)

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

        swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorScheme(Android.R.color.holo_blue_bright, 
                Android.R.color.holo_green_light, 
                Android.R.color.holo_orange_light, 
                Android.R.color.holo_red_light);
}
2
IsentropicChaos

Assurez-vous simplement que vous avez mis le code pour la mise à jour du balayage dans override fun onViewCreated(view: View, savedInstanceState: Bundle?) {} au lieu de onCreateView.

0
G_Meet1004