我似乎已经使用我的自定义ArrayAdapter撞墙了。我开始访问特定的R元素,具体取决于传入的类类型,但从那以后BOOM! (以下NPE)。麻烦的是,它没有列出我的任何代码,而且我有点失明。我知道这是我的代码,因为注释掉setListAdapter(以及其他位),运行正常。我只是不知道我做了什么。
我现在只测试ArrayAdapter,因此for循环创建对象。但至少我知道那些不是空的,因为我尽可能地完成了代码。
非常感谢任何帮助。我很确定我做了一些愚蠢的事情,但现在已经过了一个小时,而且我已经失去了密码!
非常感谢。
PS。我已经检查了其他问题,但大多数问题似乎是关于发送的空对象,或者没有正确创建布局或实例化的人。我看起来完全不同,因为LogCat没有显示任何指向我代码的链接:(
logcat的:
11-19 13:12:44.968: E/AndroidRuntime(7406): FATAL EXCEPTION: main
11-19 13:12:44.968: E/AndroidRuntime(7406): java.lang.NullPointerException
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.widget.AbsListView.obtainView(AbsListView.java:1454)
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.widget.ListView.makeAndAddView(ListView.java:1745)
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.widget.ListView.fillDown(ListView.java:670)
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.widget.ListView.fillFromTop(ListView.java:727)
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.widget.ListView.layoutChildren(ListView.java:1598)
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.widget.AbsListView.onLayout(AbsListView.java:1282)
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.view.View.layout(View.java:7175)
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.view.View.layout(View.java:7175)
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.view.View.layout(View.java:7175)
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.os.Looper.loop(Looper.java:130)
11-19 13:12:44.968: E/AndroidRuntime(7406): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-19 13:12:44.968: E/AndroidRuntime(7406): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 13:12:44.968: E/AndroidRuntime(7406): at java.lang.reflect.Method.invoke(Method.java:507)
11-19 13:12:44.968: E/AndroidRuntime(7406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-19 13:12:44.968: E/AndroidRuntime(7406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-19 13:12:44.968: E/AndroidRuntime(7406): at dalvik.system.NativeStart.main(Native Method)
ListActivity:
package com.goosesys.dta_pta_test;
import com.goosesys.gooselib.Views.GooseArrayAdapter;
import com.goosesys.gooselib.Views.GooseDeliveryItem;
import com.goosesys.gooselib.Interfaces.*;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;
public class DeliveryList extends ListActivity implements OnTaskFinished {
public String response = "";
static String[] vals;
static String[] vals2;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
populate();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id){
String selectedValue = (String)getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_LONG).show();
}
private void populate(){
//new GetFromNetTask(this).execute(AppSettings.DELIV_LIST_URL);
GooseDeliveryItem[] deliveries = new GooseDeliveryItem[50];
for(int i=0; i<50; i++){
GooseDeliveryItem gdi = new GooseDeliveryItem(i, "Some Customer " + String.valueOf(i));
deliveries[i] = gdi;
}
setListAdapter(new GooseArrayAdapter<Object>(this, R.layout.goose_delivery_item, deliveries));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
// on click stuff here
}
@Override
public void onTaskFinished(String result) {
}
}
ArrayAdapter:
package com.goosesys.gooselib.Views;
import com.goosesys.gooselib.R;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.content.Context;
public class GooseArrayAdapter<T> extends ArrayAdapter<T> {
private Context context;
private int layoutResId;
T data[] = null;
public GooseArrayAdapter(Context context, int layoutResId, T[] data){
super(context, layoutResId, data);
this.layoutResId = layoutResId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(this.layoutResId, parent, false);
T obj = data[position];
//if(obj instanceof GooseDeliveryItem){
//TextView tv = (TextView)rowView.findViewById(R.id.customerName);
//tv.setText(((GooseDeliveryItem)obj).customerText);
//}
return convertView;
}
}
我如何从主要活动中调用Intent:
GooseMenuItem gmi2 = (GooseMenuItem)findViewById(R.id.gmiDelivery);
gmi2.setHeaderText("DELIVERIES");
gmi2.setDescriptionText("Out and about");
gmi2.setButtonImageSrc(getResources().getDrawable(R.drawable.truck));
gmi2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Main.this, DeliveryList.class);
startActivity(i);
}
});
可怕的goose_delivery_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/White"
android:orientation="horizontal" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/listImage"
android:layout_width="40dp"
android:layout_height="40dp"
android:maxHeight="40dp"
android:maxWidth="40dp"
android:padding="2dp"
android:scaleType="fitCenter"
android:src="@drawable/truck" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="@+id/customerName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="CUSTOMER NAME/ADDRESS"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:orientation="vertical" >
<ImageView
android:id="@+id/mapPointImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:scaleType="centerInside"
android:src="@drawable/mappoint" />
</LinearLayout>
</TableRow>
</TableLayout>
</RelativeLayout>
答案 0 :(得分:2)
尝试将getView
更改为:
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(this.layoutResId, parent, false);
T obj = data[position];
//if(obj instanceof GooseDeliveryItem){
//TextView tv = (TextView)rowView.findViewById(R.id.customerName);
//tv.setText(((GooseDeliveryItem)obj).customerText);
//}
return rowView ;
}
返回rowView
而不是convertView
,因为永远不会分配convertView
答案 1 :(得分:2)
您的getView
函数返回null
,因为除了传递的参数之外,您永远不会返回任何内容。最初convertView
将为null,您需要初始化它。之后,如果它不为null,您可以简单地重复使用它。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
if (convertView == null) {
convertView = ... // Create you view here
}
return convertView;
}