我有一个列表,其中新闻类有标题,日期,网址,图片网址和图片alt。当我尝试访问数组位置中的特定新闻项时,它会抛出一个java.util.HashMap无法强制转换为新闻
这是Recycler Adapter http://hastebin.com/ohuvopoqiv.avrasm
编辑: 这是完整的例外
10-29 22:18:24.587 28148-28148/io.github.brady131313.shsnewspaper E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: io.github.brady131313.shsnewspaper, PID: 28148
java.lang.ClassCastException: java.util.HashMap cannot be cast to io.github.brady131313.shsnewspaper.News.NewsItem
at io.github.brady131313.shsnewspaper.News.NewsAdapter.onBindViewHolder(NewsAdapter.java:53)
at io.github.brady131313.shsnewspaper.News.NewsAdapter.onBindViewHolder(NewsAdapter.java:22)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:4048)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3366)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3258)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1803)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1302)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1265)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:522)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1918)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2155)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4868)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4868)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4868)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:887)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4868)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4868)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4868)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4868)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4868)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4868)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15745)
at android.view.ViewGroup.layout(ViewGroup.java:4868)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2330)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2043)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6567)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:573)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.
编辑: 我通过List的类
public class NewsSelectorFragment extends Fragment {
public static RecyclerView mRecyclerView;
public static Firebase firebase = new Firebase("https://shs-newspaper.firebaseio.com/");
public static List<NewsItem> mNews = new ArrayList<NewsItem>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.news_selector_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
MainActivity.toolbar.setTitle("Sherwood Arrow");
mRecyclerView = (RecyclerView) getActivity().findViewById(R.id.news_recycler);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.getRecycledViewPool().clear();
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
firebase.child("News").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mNews = (List<NewsItem>) dataSnapshot.getValue();
/*for (int i = 0; i < mNews.size(); i++) {
System.out.println(mNews.get(i));
}*/
mRecyclerView.setAdapter(new NewsAdapter(mNews));
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
}
答案 0 :(得分:0)
您链接的代码(您确实应该在此处包含相关代码而不仅仅是链接到它)以两种方式设置mNews。一个将它设置为新的ArrayList。另一个允许它像这样传递:
public NewsAdapter(List<NewsItem> news) {
this.mNews = news;
}
要获得您链接的错误(请参阅前面的唠叨)某个人已经建立了一个地图并以某种方式投射它,让他们在mNews中潜入它。只要您尝试将其用作List,它就会中断。找到它的来源。