Android:加载ListView时出错

时间:2013-05-29 05:06:54

标签: android listview nullpointerexception android-arrayadapter

我尝试使用defualt颜色绿色在我的oncreate()方法中加载我的lsitview,但它在行v.setBackgroundColor(Color.GREEN);

中给出了错误
    public class createtarget extends ListActivity
    {

        String [] Target;
        ListView lstView;


        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.createtarget);


            lstView = getListView();    
            lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);        
            lstView.setTextFilterEnabled(true);

             Target=new String []{"A","B","C"};
            final stableArrayAdapter adapter = new stableArrayAdapter(this,android.R.layout.simple_list_item_checked, Target);
            setListAdapter(adapter);


        }
}

适配器类:

class stableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public stableArrayAdapter(Context context, int textViewResourceId, String [] objects) 
    {
      super(context, textViewResourceId, objects);
      for (int i = 0; i < objects.length; ++i) {
        mIdMap.put(objects[i], i);
      }
    }

    @Override
    public long getItemId(int position) {
      String item = getItem(position);
      return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
      return true;
    }
    @Override
    public View getView(int position, View v, ViewGroup parent) 
    {


        if(v!= null)
                     v.setBackgroundColor(Color.GREEN);

        return v;
    } 

  }

错误:

05-31 04:38:20.795: E/AndroidRuntime(9525): FATAL EXCEPTION: main
05-31 04:38:20.795: E/AndroidRuntime(9525): java.lang.NullPointerException
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.widget.AbsListView.obtainView(AbsListView.java:2161)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.widget.ListView.makeAndAddView(ListView.java:1831)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.widget.ListView.fillDown(ListView.java:674)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.widget.ListView.fillFromTop(ListView.java:735)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.widget.ListView.layoutChildren(ListView.java:1652)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.widget.AbsListView.onLayout(AbsListView.java:1994)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.View.layout(View.java:14008)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.View.layout(View.java:14008)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.View.layout(View.java:14008)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.View.layout(View.java:14008)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.View.layout(View.java:14008)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.Choreographer.doFrame(Choreographer.java:532)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.os.Handler.handleCallback(Handler.java:725)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.os.Looper.loop(Looper.java:137)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at java.lang.reflect.Method.invokeNative(Native Method)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at java.lang.reflect.Method.invoke(Method.java:511)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-31 04:38:20.795: E/AndroidRuntime(9525):     at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:1)

像那样使用

View v = super.getView(position, convertView, parent);
   v.setBackgroundColor(Color.YELLOW);

答案 1 :(得分:1)

如果查看堆栈跟踪,没有提及setColor,它会在一些内部布局相关函数中崩溃。
那是因为你要覆盖getView(),因为视图不会被创建 在getView()中首先调用super.getView()然后修改并返回其结果
编辑:虽然您通常会覆盖getView()以扩充自定义布局,但原始海报只想更改背景颜色。调用super.getView(...)会让Android的实现膨胀布局,然后他可以做他的小调整。减少原始代码,减少原始错误:)

答案 2 :(得分:0)

您的color.xml文件中的Drfine绿色值,并将其用作以下内容

v.setBackgroundResource(R.color.green);

答案 3 :(得分:0)

使用适配器中的getView()方法创建视图层次结构。

在res / layout中为其定义布局,并在getView()中对其进行充气:

public View getView(int position, View v, ViewGroup parent) {
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.child_layout, parent, false);
    }
    // use v here
}