我是Android ArrayAdapters的新手,在填充ListView时我遇到了一个空指针异常。我用来填充listview的数据不是null,我已经检查过了。整个事情在第一次加载时工作正常,但是一旦我向下滚动到列表的末尾,应用程序崩溃了。
这是我的堆栈跟踪:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2386)
at android.widget.ListView.makeAndAddView(ListView.java:1769)
at android.widget.ListView.fillDown(ListView.java:672)
at android.widget.ListView.fillGap(ListView.java:636)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5201)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4358)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:524)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5059)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
这是我的活动代码:“我已经标记了我填充listView的行”
public class AddPersonActivity extends Activity
implements
PlusClient.OnPeopleLoadedListener,
PlusClient.ConnectionCallbacks,
PlusClient.OnConnectionFailedListener {
private static final String TAG = "AddPersonActivity";
private static final String STATE_RESOLVING_ERROR = "resolving_error";
private ArrayAdapter sListAdapter;
private ListView sPersonListView;
private ArrayList<String> sListItems;
private PlusClient sPlusClient;
private boolean sResolvingError;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_person);
Log.d(TAG, "onCreate called");
sPlusClient = new PlusClient.Builder(this, this, this)
.setActions(MomentUtil.ACTIONS)
.build();
sListItems = new ArrayList<String>();
sListAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1, sListItems);
sPersonListView = (ListView) findViewById(R.id.person_list);
sPersonListView.setAdapter(sListAdapter);
sResolvingError = savedInstanceState != null
&& savedInstanceState.getBoolean(STATE_RESOLVING_ERROR, false);
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart : connecting PlusClient");
sPlusClient.connect();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(STATE_RESOLVING_ERROR, sResolvingError);
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop : disconnecting PlusClient");
sPlusClient.disconnect();
}
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "onConnected : PlusClient connected");
sPlusClient.loadVisiblePeople(this, null);
}
@Override
public void onDisconnected() {
Log.d(TAG, "onDisconnected : PlusClient re-connect, PersonListView null");
sPlusClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed : connecting PlusClient");
sPlusClient.connect();
}
@Override
public void onPeopleLoaded(ConnectionResult connectionResult, PersonBuffer persons, String s) {
Log.d(TAG, "onPersonLoaded called");
switch (connectionResult.getErrorCode()) {
case ConnectionResult.SUCCESS:
sListItems.clear();
Log.d(TAG, "Loading People");
try {
int count = persons.getCount();
Log.d(TAG, String.valueOf(count));
for (int i = 0; i < count; i++) {
/*====== this is where listView is populated =====*/
sListItems.add(persons.get(i).getDisplayName());
}
} finally {
persons.close();
}
sListAdapter.notifyDataSetChanged();
break;
case ConnectionResult.SIGN_IN_REQUIRED:
sPlusClient.disconnect();
sPlusClient.connect();
break;
default:
Log.e(TAG, "Error when listing people: " + connectionResult);
break;
}
}
}
我尝试在每个阶段记录以找到可能导致这种情况的确切行,但我找不到任何东西。如果有人能让我知道我做错了什么,那会很有帮助。
提前致谢!!!
答案 0 :(得分:27)
我认为ArrayList
中的某个项目为空。这就是为什么当你向下滚动到那个项目时它会给NullPointerException
。如果在将任何项目添加到ListView
之前检查是否为空,那会更好。
int count = persons.getCount();
Log.d(TAG, String.valueOf(count));
for (int i = 0; i < count; i++) {
// this is where listView is populated
if(persons.get(i).getDisplayName() != null) {
sListItems.add(persons.get(i).getDisplayName());
}
}
答案 1 :(得分:6)
由于您已经提到在第一次加载时填充了列表。只有在滚动发生时,应用才会崩溃。您人员列表中的一项是null
。