gpt4 book ai didi

来自 Array List 的 Android ListAdapter

转载 作者:搜寻专家 更新时间:2023-11-01 08:09:54 26 4
gpt4 key购买 nike

我的公司有一队送货卡车,他们希望将其视为:1) Android 屏幕上半部分 map 上的图钉;和 2) 在同一屏幕的下半部分包含卡车和司机详细信息的 ListView。我有一个 Web 服务,它为我提供了卡车的“名称”、它的口号(我们司机的昵称)以及存储在名为“TruckArray”的 ArrayList 中的地理点位置。我在 FrameLayout 中创建了一个包含 mapview 和 listview 的 main.xml 布局,扩展了 MapView 类,获得了地理位置并创建了一个 map 叠加层来显示每辆卡车的位置。我的屏幕正确显示了 map 和卡车位置(如果我在下面注释掉对 ListAdapter 的调用)。我的问题是我调用设置 ListAdapter 错误并强制关闭我的应用程序,既不显示 map 也不显示 listarray。

如果您能为这位 Android 新手提供任何帮助,我们将不胜感激!

初始化 ListView 的调用是:

public void initList() {


listView = null;
ListView listView = (ListView) findViewById(R.id.mylist);
adapter = new TruckAdapter(this, R.id.mylist, TruckArray);
listView.setAdapter(adapter); // If I comment out this line, the map appears. If I uncomment it, the whole app force closes before the map is drawn.
}

我的卡车适配器是:

public class TruckAdapter extends ArrayAdapter<Truck> {

private ArrayList<Truck> trucks;
private Context context;

public TruckAdapter(Context context, int textViewResourceId, ArrayList<Truck> objects){
super(context, textViewResourceId, objects);
this.trucks = objects;
}

public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;

if (view == null) {
LayoutInflater viewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = viewInflater.inflate(R.layout.row, null);
}

Truck truck = trucks.get(position);

if (truck != null) {
TextView name = (TextView) view.findViewById(R.id.name);
TextView slogan = (TextView) view.findViewById(R.id.slogan);

if (name != null) {
name.setText(truck.name);
}

if (slogan != null){
slogan.setText(truck.slogan);
}
}
return view;
}
}

我的 ROW.XML 文件是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textStyle="bold"
android:maxLines="1"
android:ellipsize="end"
/>
<TextView android:id="@+id/slogan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:maxLines="1"
android:ellipsize="end"
/>
</LinearLayout>
</LinearLayout>

我的 MAIN.XML 文件是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="2" >

<FrameLayout android:id="@+id/mapContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >

<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:apiKey="my APIkey is here..."
android:clickable="true" />
</FrameLayout>

<FrameLayout android:layout_width="match_parent"
android:layout_height="0dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="1" >
<ListView android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center_horizontal" />
</FrameLayout>

</LinearLayout>

我的崩溃日志是:

05-30 20:17:49.754: W/CursorWrapperInner(30659): Cursor finalized without prior close()
05-30 20:17:49.965: D/dalvikvm(30659): GC_CONCURRENT freed 717K, 48% free 3783K/7175K, external 2357K/2773K, paused 2ms+3ms
05-30 20:17:50.035: D/dalvikvm(30659): GC_EXTERNAL_ALLOC freed 528K, 52% free 3447K/7175K, external 2757K/2773K, paused 34ms
05-30 20:17:50.105: D/AndroidRuntime(30659): Shutting down VM
05-30 20:17:50.105: W/dalvikvm(30659): threadid=1: thread exiting with uncaught exception (group=0x40018560)
05-30 20:17:50.105: E/AndroidRuntime(30659): FATAL EXCEPTION: main
05-30 20:17:50.105: E/AndroidRuntime(30659): java.lang.NullPointerException
05-30 20:17:50.105: E/AndroidRuntime(30659): at com.deltruck.DlvrTruckMain$TruckAdapter.getView(DlvrTruckMain.java:273)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.widget.AbsListView.obtainView(AbsListView.java:1456)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.widget.ListView.makeAndAddView(ListView.java:1821)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.widget.ListView.fillDown(ListView.java:686)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.widget.ListView.fillFromTop(ListView.java:754)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.widget.ListView.layoutChildren(ListView.java:1670)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.widget.AbsListView.onLayout(AbsListView.java:1286)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.view.View.layout(View.java:7184)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.view.View.layout(View.java:7184)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.view.View.layout(View.java:7184)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.view.View.layout(View.java:7184)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.view.View.layout(View.java:7184)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.view.View.layout(View.java:7184)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.os.Looper.loop(Looper.java:130)
05-30 20:17:50.105: E/AndroidRuntime(30659): at android.app.ActivityThread.main(ActivityThread.java:3806)
05-30 20:17:50.105: E/AndroidRuntime(30659): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 20:17:50.105: E/AndroidRuntime(30659): at java.lang.reflect.Method.invoke(Method.java:507)
05-30 20:17:50.105: E/AndroidRuntime(30659): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-30 20:17:50.105: E/AndroidRuntime(30659): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-30 20:17:50.105: E/AndroidRuntime(30659): at dalvik.system.NativeStart.main(Native Method)
05-30 20:17:53.238: I/Process(30659): Sending signal. PID: 30659 SIG: 9

最佳答案

首先.. 我们可以从堆栈跟踪中看到问题出在适配器的第 273 行,在您的 getView() 方法中。但是我们不知道第 273 行是哪一行......如果你能告诉我们那一行的代码是什么,那就太好了:)

现在回到问题。

LayoutInflater viewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

此上下文为 null ;) 您将其作为成员变量,但从未对其进行初始化。

编辑:您不必将 ArrayList 存储为成员。对 super 构造函数的调用会处理这一点,您可以使用 getItem(int position) 从 ArrayList 中获取一个项目

关于来自 Array List 的 Android ListAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10825886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com