android.widget.RelativeLayout $的LayoutParams不能转换为android.widget.AbsListView $的LayoutParams [英] android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
问题描述
下面有无数的教程和示例项目,我在下面的code,应填写一个ListView与包含了行对象的多个值的行来了。
作为一个新手程序员,我不能为我的生命找出其中的地狱我的code失败。任何帮助将是非常美联社preciated!
如果我离开了重要的信息,请告诉我,我会更新这个问题。
在code启动它所有的片段里面:
@覆盖
公共无效onActivityCreated(捆绑savedInstanceState){
super.onActivityCreated(savedInstanceState);
意向I = getActivity()getIntent()。 如果(i.getSerializableExtra(ChosenWorkorder)!= NULL){ 工作单selectedWorkorder =(工作单)i.getSerializableExtra(ChosenWorkorder); ArticlesListAdapter articlesListAdapter =新ArticlesListAdapter(getActivity(),R.layout.articles_list_row,selectedWorkorder.GetAllLines()); 。ListView控件articlesList =(ListView控件)getActivity()findViewById(R.id.ArticlesList);
articlesList.setAdapter(articlesListAdapter); }
}
自定义ArrayAdapter:
公共类ArticlesListAdapter扩展ArrayAdapter<直线和GT; { 私人列表<直线和GT; lineList;
私人上下文的背景下; 公共ArticlesListAdapter(上下文的背景下,INT textViewResourceId,列表与LT;线>对象){
超(背景下,textViewResourceId,对象);
this.lineList =物体;
this.context =背景;
} 公众诠释的getCount(){
返回lineList.size();
} 公共线路的getItem(INT位置){
返回lineList.get(位置);
} 众长getItemId(INT位置){
返回的位置;
} @燮pressLint(ViewHolder)/ *阙? * /
公共查看getView(INT位置,查看convertView,父母的ViewGroup){ LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看rowView = inflater.inflate(R.layout.articles_list_row,父母,假); TextView的articleName =(TextView中)rowView.findViewById(R.id.textArticleName);
的EditText articleAmount =(EditText上)rowView.findViewById(R.id.textArticleAmount); 线articleLine = lineList.get(位置); articleName.setText(articleLine.getLineArticleDescription());
articleAmount.setText(articleLine.getLineArticleAmount()的toString()); 回到父母;
}
}
下面是LogCat中日志澄清(对我来说这意味着什么)。
E / AndroidRuntime(15941):致命异常:主要
E / AndroidRuntime(15941):工艺:be.mabolifting.maboserve,PID:15941
E / AndroidRuntime(15941):java.lang.ClassCastException:android.widget.RelativeLayout $的LayoutParams不能转换为android.widget.AbsListView $的LayoutParams
E / AndroidRuntime(15941):在android.widget.ListView.setupChild(ListView.java:1826)
E / AndroidRuntime(15941):在android.widget.ListView.makeAndAddView(ListView.java:1793)
E / AndroidRuntime(15941):在android.widget.ListView.fillDown(ListView.java:691)
E / AndroidRuntime(15941):在android.widget.ListView.fillFromTop(ListView.java:752)
E / AndroidRuntime(15941):在android.widget.ListView.layoutChildren(ListView.java:1630)
E / AndroidRuntime(15941):在android.widget.AbsListView.onLayout(AbsListView.java:2087)
E / AndroidRuntime(15941):在android.view.View.layout(View.java:14860)
E / AndroidRuntime(15941):在android.view.ViewGroup.layout(ViewGroup.java:4643)
E / AndroidRuntime(15941):在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
E / AndroidRuntime(15941):在android.view.View.layout(View.java:14860)
E / AndroidRuntime(15941):在android.view.ViewGroup.layout(ViewGroup.java:4643)
E / AndroidRuntime(15941):在android.support.v4.view.ViewPager.onLayout(ViewPager.java:1594)
E / AndroidRuntime(15941):在android.view.View.layout(View.java:14860)
E / AndroidRuntime(15941):在android.view.ViewGroup.layout(ViewGroup.java:4643)
E / AndroidRuntime(15941):在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
E / AndroidRuntime(15941):在android.widget.FrameLayout.onLayout(FrameLayout.java:388)
E / AndroidRuntime(15941):在android.view.View.layout(View.java:14860)
E / AndroidRuntime(15941):在android.view.ViewGroup.layout(ViewGroup.java:4643)
E / AndroidRuntime(15941):在com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
E / AndroidRuntime(15941):在android.view.View.layout(View.java:14860)
E / AndroidRuntime(15941):在android.view.ViewGroup.layout(ViewGroup.java:4643)
E / AndroidRuntime(15941):在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
E / AndroidRuntime(15941):在android.widget.FrameLayout.onLayout(FrameLayout.java:388)
E / AndroidRuntime(15941):在android.view.View.layout(View.java:14860)
E / AndroidRuntime(15941):在android.view.ViewGroup.layout(ViewGroup.java:4643)
E / AndroidRuntime(15941):在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2013)
E / AndroidRuntime(15941):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1770)
E / AndroidRuntime(15941):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
E / AndroidRuntime(15941):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5725)
E / AndroidRuntime(15941):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761)
E / AndroidRuntime(15941):在android.view.Choreographer.doCallbacks(Choreographer.java:574)
E / AndroidRuntime(15941):在android.view.Choreographer.doFrame(Choreographer.java:544)
E / AndroidRuntime(15941):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747)
E / AndroidRuntime(15941):在android.os.Handler.handleCallback(Handler.java:733)
E / AndroidRuntime(15941):在android.os.Handler.dispatchMessage(Handler.java:95)
E / AndroidRuntime(15941):在android.os.Looper.loop(Looper.java:136)
E / AndroidRuntime(15941):在android.app.ActivityThread.main(ActivityThread.java:5086)
E / AndroidRuntime(15941):在java.lang.reflect.Method.invokeNative(本机方法)
E / AndroidRuntime(15941):在java.lang.reflect.Method.invoke(Method.java:515)
E / AndroidRuntime(15941):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)
E / AndroidRuntime(15941):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
E / AndroidRuntime(15941):在dalvik.system.NativeStart.main(本机方法)
在你的 getView()
,更改
回报父母;
到
收益rowView;
getView()
应该返回其中的行被放置在该行视图,而不是父,父视图仅作为paramteter提供这样认为通胀能处理与 match_parent
尺寸和这样的。
Following numerous tutorials and example projects I've arrived at the following code which should fill a ListView with rows that contain multiple values out of a Line object.
Being a newbie programmer I cannot for the life of me figure out where the hell my code fails. Any help would be much appreciated!
If I left out important information please tell me and I will update this question.
The code that starts it all inside the Fragment:
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
Intent i = getActivity().getIntent();
if (i.getSerializableExtra("ChosenWorkorder") != null){
Workorder selectedWorkorder = (Workorder) i.getSerializableExtra("ChosenWorkorder");
ArticlesListAdapter articlesListAdapter = new ArticlesListAdapter(getActivity(), R.layout.articles_list_row, selectedWorkorder.GetAllLines());
ListView articlesList = (ListView) getActivity().findViewById(R.id.ArticlesList);
articlesList.setAdapter(articlesListAdapter);
}
}
The Custom ArrayAdapter:
public class ArticlesListAdapter extends ArrayAdapter<Line> {
private List<Line> lineList;
private Context context;
public ArticlesListAdapter(Context context, int textViewResourceId, List<Line> objects) {
super(context, textViewResourceId, objects);
this.lineList = objects;
this.context = context;
}
public int getCount() {
return lineList.size();
}
public Line getItem(int position) {
return lineList.get(position);
}
public long getItemId(int position) {
return position;
}
@SuppressLint("ViewHolder") /* Que? */
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.articles_list_row, parent, false);
TextView articleName = (TextView) rowView.findViewById(R.id.textArticleName);
EditText articleAmount = (EditText) rowView.findViewById(R.id.textArticleAmount);
Line articleLine = lineList.get(position);
articleName.setText(articleLine.getLineArticleDescription());
articleAmount.setText(articleLine.getLineArticleAmount().toString());
return parent;
}
}
Below is the LogCat log for clarification (to me it means nothing).
E/AndroidRuntime(15941): FATAL EXCEPTION: main
E/AndroidRuntime(15941): Process: be.mabolifting.maboserve, PID: 15941
E/AndroidRuntime(15941): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
E/AndroidRuntime(15941): at android.widget.ListView.setupChild(ListView.java:1826)
E/AndroidRuntime(15941): at android.widget.ListView.makeAndAddView(ListView.java:1793)
E/AndroidRuntime(15941): at android.widget.ListView.fillDown(ListView.java:691)
E/AndroidRuntime(15941): at android.widget.ListView.fillFromTop(ListView.java:752)
E/AndroidRuntime(15941): at android.widget.ListView.layoutChildren(ListView.java:1630)
E/AndroidRuntime(15941): at android.widget.AbsListView.onLayout(AbsListView.java:2087)
E/AndroidRuntime(15941): at android.view.View.layout(View.java:14860)
E/AndroidRuntime(15941): at android.view.ViewGroup.layout(ViewGroup.java:4643)
E/AndroidRuntime(15941): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
E/AndroidRuntime(15941): at android.view.View.layout(View.java:14860)
E/AndroidRuntime(15941): at android.view.ViewGroup.layout(ViewGroup.java:4643)
E/AndroidRuntime(15941): at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1594)
E/AndroidRuntime(15941): at android.view.View.layout(View.java:14860)
E/AndroidRuntime(15941): at android.view.ViewGroup.layout(ViewGroup.java:4643)
E/AndroidRuntime(15941): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
E/AndroidRuntime(15941): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
E/AndroidRuntime(15941): at android.view.View.layout(View.java:14860)
E/AndroidRuntime(15941): at android.view.ViewGroup.layout(ViewGroup.java:4643)
E/AndroidRuntime(15941): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
E/AndroidRuntime(15941): at android.view.View.layout(View.java:14860)
E/AndroidRuntime(15941): at android.view.ViewGroup.layout(ViewGroup.java:4643)
E/AndroidRuntime(15941): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
E/AndroidRuntime(15941): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
E/AndroidRuntime(15941): at android.view.View.layout(View.java:14860)
E/AndroidRuntime(15941): at android.view.ViewGroup.layout(ViewGroup.java:4643)
E/AndroidRuntime(15941): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2013)
E/AndroidRuntime(15941): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1770)
E/AndroidRuntime(15941): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
E/AndroidRuntime(15941): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
E/AndroidRuntime(15941): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
E/AndroidRuntime(15941): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
E/AndroidRuntime(15941): at android.view.Choreographer.doFrame(Choreographer.java:544)
E/AndroidRuntime(15941): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
E/AndroidRuntime(15941): at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime(15941): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(15941): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(15941): at android.app.ActivityThread.main(ActivityThread.java:5086)
E/AndroidRuntime(15941): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(15941): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(15941): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
E/AndroidRuntime(15941): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
E/AndroidRuntime(15941): at dalvik.system.NativeStart.main(Native Method)
In your getView()
, change
return parent;
to
return rowView;
getView()
should return the row view and not the parent where the rows are placed in. The parent view is only supplied as a paramteter so that view inflation can deal with match_parent
sizes and such.
这篇关于android.widget.RelativeLayout $的LayoutParams不能转换为android.widget.AbsListView $的LayoutParams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!