我已经在Android上尝试了几个星期了,但是在使用自定义对象填充listview时遇到了麻烦。我已经尝试了几个有关堆栈溢出和youtube的教程,但是当应用程序运行时,它会在list.setAdapter(适配器)崩溃。但我不清楚原因是什么。
这是我最近的尝试:
public class NPCEditor extends Fragment{
View myView;
ArrayList<NPC> NPCs = new ArrayList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.npc_edit, container, false);
populateNPCList();
populateListview();
return myView;
}
private void populateNPCList(){
NPCs.add(new NPC(40, 10, 10, 10, 10, 10, 10, "", "", "", "", "", "", ""));
NPCs.add(new NPC(50, 10, 10, 10, 10, 10, 10, "", "", "", "", "", "", ""));
NPCs.add(new NPC(60, 10, 10, 10, 10, 10, 10, "", "", "", "", "", "", ""));
NPCs.add(new NPC(70, 10, 10, 10, 10, 10, 10, "", "", "", "", "", "", ""));
NPCs.add(new NPC(80, 10, 10, 10, 10, 10, 10, "", "", "", "", "", "", ""));
}
private void populateListview(){
ArrayAdapter<NPC> adapter = new NPCAdapter2();
ListView list = myView.findViewById(R.id.NPCList);
list.setAdapter(adapter);
}
private class NPCAdapter2 extends ArrayAdapter<NPC>{
public NPCAdapter2(){
super(getActivity(), R.layout.npc_layout, NPCs);
}
public View getView(int position, View convertView, ViewGroup parent){
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.npc_layout, parent, false);
}
NPC currentNPC = NPCs.get(position);
TextView txtAge = itemView.findViewById(R.id.textAge);
txtAge.setText(currentNPC.getAge());
return itemView;
}
}
}
和xml文件 npc_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Age" />
</LinearLayout>
和NPCEditor布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/NPCList"
android:layout_width="368dp"
android:layout_height="327dp"
android:layout_marginBottom="16dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
Logcat:
崩溃开始
04-22 10:43:56.986 12587-12587 / blackanvil.com.dmi E / AndroidRuntime:&gt;致命异议:主要 流程:blackanvil.com.dmi,PID:12587
java.lang.NoSuchMethodError:没有虚方法 getLayoutInflater()Landroid /视图/ LayoutInflater;在班上 Lblackanvil / COM / DMI / NPCEditor;或其超级类(声明 &#39; blackanvil.com.dmi.NPCEditor&#39;出现在 /data/app/blackanvil.com.dmi-1/split_lib_slice_2_apk.apk) at blackanvil.com.dmi.NPCEditor $ NPCAdapter2.getView(NPCEditor.java:53) 在android.widget.AbsListView.obtainView(AbsListView.java:2497) 在android.widget.ListView.makeAndAddView(ListView.java:1930) 在android.widget.ListView.fillDown(ListView.java:722) 在android.widget.ListView.fillFromTop(ListView.java:783) 在android.widget.ListView.layoutChildren(ListView.java:1721) 在android.widget.AbsListView.onLayout(AbsListView.java:2254) 在android.view.View.layout(View.java:16966) 在android.view.ViewGroup.layout(ViewGroup.java:5604) 在 android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1855) 在android.view.View.layout(View.java:16966) 在android.view.ViewGroup.layout(ViewGroup.java:5604) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:398) 在android.widget.FrameLayout.onLayout(FrameLayout.java:333) 在android.view.View.layout(View.java:16966) 在android.view.ViewGroup.layout(ViewGroup.java:5604) 在 android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1855) 在android.view.View.layout(View.java:16966) 在android.view.ViewGroup.layout(ViewGroup.java:5604) 在 android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132) 在 android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) 在 android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361) 在 android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:869) 在android.view.View.layout(View.java:16966) 在android.view.ViewGroup.layout(ViewGroup.java:5604) 在 android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1172) 在android.view.View.layout(View.java:16966) 在android.view.ViewGroup.layout(ViewGroup.java:5604) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:398) 在android.widget.FrameLayout.onLayout(FrameLayout.java:333) 在android.view.View.layout(View.java:16966) 在android.view.ViewGroup.layout(ViewGroup.java:5604) 在android.widget.LinearLayout.setChildFrame(LinearLayout.java:2022) 在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1865) 在android.widget.LinearLayout.onLayout(LinearLayout.java:1774) 在android.view.View.layout(View.java:16966) 在android.view.ViewGroup.layout(ViewGroup.java:5604) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:398) 在android.widget.FrameLayout.onLayout(FrameLayout.java:333) 在android.view.View.layout(View.java:16966) 在android.view.ViewGroup.layout(ViewGroup.java:5604) 在android.widget.LinearLayout.setChildFrame(LinearLayout.java:2022) 在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1865) 在android.widget.LinearLayout.onLayout(LinearLayout.java:1774) 在android.view.View.layout(View.java:16966) 在android.view.ViewGroup.layout(ViewGroup.java:5604) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:398) 在android.widget.FrameLayout.onLayout(FrameLayout.java:333) 在 com.android.internal.policy.PhoneWindow $ DecorView.onLayout(PhoneWindow.java:2817) 在android.view.View.layout(View.java:16966) 在android.view.ViewGroup.layout(ViewGroup.java:5604) 在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2635) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2329) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1367) 在 android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6974) 在 android.view.Choreographer $ CallbackRecord.run(Choreographer.java:922) 在android.view.Choreographer.doCallbacks(Choreographer.java:724) 在android.view.Choreographer.doFrame(Choreographer.java:658) 在android.view.Ch 04-22 10:43:57.117 12587-12587 / blackanvil.com.dmi I /处理:发送信号。 PID:12587 SIG:9
答案 0 :(得分:0)
试一试:
itemView = LayoutInflater.from(getContext()).inflate(R.layout.npc_layout, parent, false);
答案 1 :(得分:0)
我发现这个教程就像魅力一样构成了我。希望其他人发现它同样有用。
https://www.sitepoint.com/custom-data-layouts-with-your-own-android-arrayadapter/