我在NullPointerException项上调用ListView时收到了setBackgroundResource

最初,我有一个显示良好的项目列表,当用户单击这些列表项目时,基本上是OnClickListener,它们将立即更改背景颜色。但是我现在想做的是当我必须更改某些项目onCreate的背景颜色但不等待用户单击该项目时。投诉的行在setBackgroundResource()的行上。

请帮助我解决这个问题。谢谢。下面是我的代码。

    private ArrayList<String> initialfetch = new ArrayList<String>();
    private ListView mainListView;
    private ArrayAdapter<String> listAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listlayout);

        mainListView = (ListView) findViewById( R.id.list );
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, contactlist);
        mainListView.setAdapter(listAdapter);


       // get Intent from the previous calling activity
       Intent intent = getIntent();
       String identifier = intent.getStringExtra("identifier");
       String position = intent.getStringExtra("position");

       if(identifier.equals("edit"))
       { // model.Static_Activity.event_list is tested to pass the correct position of item into the list
        for(int i = 0; i < model.Static_Activity.event_list.get(Integer.parseInt(position)).getAttendees().size(); i++)
        {
            for(int j = 0; j < initialfetch.size(); j++)
            {
                if(initialfetch.get(j).equals(model.Static_Activity.event_list.get(Integer.parseInt(position)).getAttendees().get(i)))
                {   // Problems happened on this line
                    ((View) ((ListView) mainListView.getChildAt(j)).getAdapter()).setBackgroundResource(R.color.color_2);

                }
            }
        }
    }

}


$ Edited Post:以下是我的堆栈跟踪文件,第66行是setBackGroundResource行:

    08-22 06:35:32.259: E/AndroidRuntime(1749): FATAL EXCEPTION: main
    08-22 06:35:32.259: E/AndroidRuntime(1749): java.lang.RuntimeException: Unable to start         activity ComponentInfo{com.example.socialeventplanner/view.Select_Attendees}:         java.lang.NullPointerException
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.ActivityThread.access$600(ActivityThread.java:141)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.os.Handler.dispatchMessage(Handler.java:99)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.os.Looper.loop(Looper.java:137)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.ActivityThread.main(ActivityThread.java:5041)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         java.lang.reflect.Method.invokeNative(Native Method)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         java.lang.reflect.Method.invoke(Method.java:511)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         dalvik.system.NativeStart.main(Native Method)
    08-22 06:35:32.259: E/AndroidRuntime(1749): Caused by: java.lang.NullPointerException
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         view.Select_Attendees.onCreate(Select_Attendees.java:66)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.Activity.performCreate(Activity.java:5104)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    08-22 06:35:32.259: E/AndroidRuntime(1749):     at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

最佳答案

您正在刚刚创建的ListView上调用getChildAt(),它还没有子级。还要注意,如果用户向上或向下滚动ListView,则其视图将被回收,因此基本上只存在可见的视图。

同样,即使您没有获得NPE,当尝试将从ArrayAdapter返回的getAdapter()转换为View时,也会获得ClassCastException。此处完全不需要getAdapter呼叫。

您真正应该做的是创建一个自定义适配器:

private class MyAdapter extends ArrayAdapter<String> {
    public MyAdapter() {
        super(MyActivity.this, R.layout.simplerow, contactlist);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);

        if (some_condition)
            v.setBackgroundResource(R.color.color_2);

        return v;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    listAdapter = new MyAdapter();
    mainListView.setAdapter(listAdapter);
    ...
    // remove your setBackgroundResource code from here
}

08-18 10:10