Java ClassCastException:无法将android.support.v4.widget.swiperFreshLayout强制转换为android.widget.ListView
有时我的应用程序会崩溃。我不知道为什么有时它不起作用,我没有改变任何事情。在我的片段中,我在onViewCreated()上添加了两项 错误就在这附近(第60行)listview\u products=(listview)view.findviewbyd(R.id.listview\u products\u list)强> 这是我的布局文件:Java ClassCastException:无法将android.support.v4.widget.swiperFreshLayout强制转换为android.widget.ListView,java,android,listview,classcastexception,Java,Android,Listview,Classcastexception,有时我的应用程序会崩溃。我不知道为什么有时它不起作用,我没有改变任何事情。在我的片段中,我在onViewCreated()上添加了两项 错误就在这附近(第60行)listview\u products=(listview)view.findviewbyd(R.id.listview\u products\u list) 这是我的布局文件: <?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http
<?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:致命异常:主
流程:de.app.me.de,PID:22103
java.lang.RuntimeException:无法启动活动组件信息{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)
运行(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;
导入android.app.Fragment;
导入android.app.FragmentManager;
导入android.app.ProgressDialog;
导入android.content.ClipData;
导入android.content.Context;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.v4.widget.swiperFreshLayout;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.ListView;
导入android.widget.TextView;
导入org.json.simple.ItemList;
导入org.json.simple.JSONArray;
导入org.json.simple.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.Map;
导入api。请求;
导入classes.app.m
<?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>
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) {
...
}
}
}
listview_products = (ListView) rootView.findViewById(R.id.listview_product_list);
mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.productlist_swipe_refresh);