安卓错误:膨胀类SwipeRefreshLayout

24

我正在开发一个应用程序,使用了三个带有片段的选项卡,并尝试在每个片段中实现SwipeRefreshLayout。我相信我已经正确地创建了这个功能,但我不断收到以下错误:

android.view.InflateException: Binary XML file line #1: Error inflating class SwipeRefreshLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.SwipeRefreshLayout" on path: DexPathList[[zip file "/data/app/com.ryan.brooks.fropllc.frop.app-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.ryan.brooks.fropllc.frop.app-1, /vendor/lib, /system/lib]]
    at com.ryan.brooks.fropllc.frop.app.whatsGoingOnFragment.onCreateView(whatsGoingOnFragment.java:23)

我完全不知道是什么原因导致了这个问题。如果有人能够帮助我,那将不胜感激。

这是我实现SwipeRefreshLayout的片段布局:

<SwipeRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swipe_refresh_whats_going_on">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#343434"></ScrollView>

</SwipeRefreshLayout>

这是我的片段类,在其中调用了SwipeRefreshLayout。

public class WhatsGoingOnFragment extends Fragment {

    private SwipeRefreshLayout swipeLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.whats_going_on_fragment, container, false);

        // Retrieve the SwipeRefreshLayout and ListView instances
        swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_whats_going_on);

        // Set the color scheme of the SwipeRefreshLayout by providing 4 color resource ids
        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;
    }
}

所有三个片段的布局和类结构完全相同,只是ID名称不同。我真的不知道出了什么问题。再次感谢任何帮助!


4
我可能大错特错,但你可以尝试在xml文件中使用android.support.v4.widget.SwipeRefreshLayout,不改变原意并使内容更通俗易懂。 - hypd09
@ hypd09,你是完全正确的! - Rbro112
1个回答

62

你需要使用完整的包名来引用SwipeRefreshLayout:

<android.support.v4.widget.SwipeRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ryan.brooks.fropllc.frop.app.whatsGoingOnFragment"
    android:id="@+id/swipe_refresh_whats_going_on">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#343434"></ScrollView>

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

工作得非常完美,只是一个简单的错误。非常感谢你! - Rbro112
3
我正在使用ActionBarSherlock,但我无法在xml中使用SwipeRefreshLayout(虽然我可以正确导入并在代码中使用它)。有任何想法吗? - An-droid
太棒了,分辨率很小! - Digital Alchemist

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接