【问题标题】:ClassCastException: android.support.v4.widget.SwipeRefreshLayout cannot be cast to android.widget.ListViewClassCastException:android.support.v4.widget.SwipeRefreshLayout 无法转换为 android.widget.ListView
【发布时间】:2016-01-31 14:04:46
【问题描述】:

有时我的应用会崩溃。我不知道为什么它有时会起作用,我没有改变任何东西。在我的片段中,我在 onViewCreated()

上添加了 2 个项目
listview_products = (ListView) view.findViewById(R.id.listview_product_list);
mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.productlist_swipe_refresh);

错误就在这附近(第 60 行)listview_products = (ListView) view.findViewById(R.id.listview_product_list);

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/productlist_swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listview_product_list"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</android.support.v4.widget.SwipeRefreshLayout>

为什么我得到错误?我能做些什么来解决它?

01-31 14:50:09.583 22103-22103/de.app.me.de E/AndroidRuntime: 致命异常: main 进程:de.app.me.de,PID:22103 java.lang.RuntimeException:无法启动活动 ComponentInfo{de.app.me.de/de.app.me.de.account_overview}:java.lang.ClassCastException:android.support.v4.widget.SwipeRefreshLayout 无法转换为android.widget.ListView 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) 在 android.app.ActivityThread.access$900(ActivityThread.java:172) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:145) 在 android.app.ActivityThread.main(ActivityThread.java:5832) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 引起:java.lang.ClassCastException: android.support.v4.widget.SwipeRefreshLayout 不能转换为 android.widget.ListView 在 de.app.me.de.FragmentProductList.onViewCreated(FragmentProductList.java:60) 在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:918) 在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082) 在 android.app.BackStackRecord.run(BackStackRecord.java:833) 在 android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) 在 android.app.Activity.performStart(Activity.java:6257) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2621) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) 在 android.app.ActivityThread.access$900(ActivityThread.java:172) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:145) 在 android.app.ActivityThread.main(ActivityThread.java:5832) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

感谢您的帮助!

编辑:

package de.app.me.de;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.ProgressDialog;
import android.content.ClipData;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import org.json.simple.ItemList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import api.Request;
import classes.app.me.de.ListViewCustomAdapter;
import classes.app.me.de.ListViewItem;

/**
 * Created by ME on 15.01.2016.
 */
public class FragmentProductList extends Fragment{

    public FragmentProductList() {}
    private Context context;
    private TextView textView;
    private View rootView;
    private Request r;
    public ArrayList<ListViewItem> arrayList = new ArrayList<ListViewItem>();
    private ListView listview_products;
    SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        this.rootView = inflater.inflate(R.layout.fragment_productlist, container, false);
        this.context = rootView.getContext();


        return this.rootView;
    }


    @Override
    public void onViewCreated(View view, Bundle saBundle) {
        getActivity().setTitle(R.string.title_activity_account_plist);

        listview_products = (ListView) view.findViewById(R.id.listview_product_list);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.productlist_swipe_refresh);

        listview_products.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Bundle bundle = new Bundle();

                bundle.putInt("pid", arrayList.get(position).getId());

                Fragment f = new FragmentProductManager();
                f.setArguments(bundle);

                FragmentManager fm = getFragmentManager();
                fm.beginTransaction().replace(R.id.frame_container, f).commit();

            }
        });


        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                Update();

            }
        });

        Update();
    }

    public void Update () {

        arrayList.clear();

        new GetProductList().execute();
        mSwipeRefreshLayout.setRefreshing(false);

    }

    public void UpdateList (ArrayList list) {

        ListViewCustomAdapter adapter = new ListViewCustomAdapter(rootView.getContext(), list);

        listview_products.setAdapter(adapter);


    }

    private class GetProductList extends AsyncTask<String, Void, Boolean> {

        private ProgressDialog pd;

        private android.app.AlertDialog.Builder dlgAlert;
        @Override
        protected Boolean doInBackground(String... params) {

            ...

        }

        @Override
        protected void onPreExecute() {

            ...

        }

        @Override
        protected void onPostExecute(Boolean status) {

        ...

        }
    }

}

【问题讨论】:

  • 你能发布更多代码 FragmentProductList.java

标签: java android listview classcastexception


【解决方案1】:

移动此代码

    listview_products = (ListView) rootView.findViewById(R.id.listview_product_list);
mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.productlist_swipe_refresh);

在onCreateView() 方法内而不是onViewCreated()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 2023-02-02
    • 2016-06-30
    • 2016-01-14
    相关资源
    最近更新 更多