Android Fragment中的滑动刷新布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25245088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swipe Refresh Layout in Android Fragment
提问by Nacho
Im trying to implement SwipeRefreshLayout on a Fragment.
我试图在 Fragment 上实现 SwipeRefreshLayout。
I already solved it to implement it on an Activity, but when I implement it on Fragment: java.lang.NoClassDefFoundError: com...
我已经解决了它以在 Activity 上实现它,但是当我在 Fragment 上实现它时:java.lang.NoClassDefFoundError: com...
Does anyone has an idea? Thanks!
有没有人有想法?谢谢!
Similar post without answers/solution: Swipe Refresh Layout - Class not found
没有答案/解决方案的类似帖子:Swipe Refresh Layout - Class not found
My 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;
}
}
And my layout:
而我的布局:
<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>
Full stack trace
完整的堆栈跟踪
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.access0(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)
回答by Nacho
Solved it :)
解决了:)
My XML
我的 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>
My 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();
}
回答by IsentropicChaos
Put your swipeLayout initialization in onViewCreated(View view, Bundle bundle) instead of onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
将您的 swipeLayout 初始化放在 onViewCreated(View view, Bundle bundle) 而不是 onCreateView(LayoutInflater inflater, ViewGroup container, 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);
}
回答by G_Meet1004
Just be sure that you put the code for swipe refresh layout in override fun onViewCreated(view: View, savedInstanceState: Bundle?) {}
instead of onCreateView
.
只要确保您将滑动刷新布局的代码放在override fun onViewCreated(view: View, savedInstanceState: Bundle?) {}
而不是onCreateView
.