web-dev-qa-db-ja.com

Androidフラグメントでレイアウトをスワイプ更新

フラグメントにSwipeRefreshLayoutを実装しようとしています。

私はすでにそれをアクティビティに実装するように解決しましたが、フラグメントに実装すると:Java.lang.NoClassDefFoundError:com ...

誰かがアイデアを持っていますか?ありがとう!

回答/解決策のない同様の投稿: スワイプレイアウトの更新-クラスが見つかりません

私のコード:

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;
    }
}

そして私のレイアウト:

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

完全なスタックトレース

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

解決しました:)

私の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>

私の断片

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

SwipeLayoutの初期化をonCreateView(LayoutInflater inflater、ViewGroup container、Bundle savedInstanceState)ではなくonViewCreated(View view、Bundle bundle)に入れます。

@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

onCreateViewではなくoverride fun onViewCreated(view: View, savedInstanceState: Bundle?) {}にスワイプリフレッシュレイアウトのコードを配置してください。

0
G_Meet1004