Android小部件ArrayAdapter中的NullPointerException createViewFromResource



Answers

既然你已經提到這個列表在第一次加載時被填充。 只有當滾動發生時,應用程序崩潰。 人員列表中的其中一項為null

Question

我是一個新的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;
    }
}
}

我試圖在每一個階段的日誌記錄,以找到可能導致這個問題的確切的線,但我沒有找到。 如果有人能讓我知道我做錯了什麼,這將是很多的幫助。

提前致謝!!!




Related