1

我尝试oncreate()使用默认颜色Green在我的方法中加载我的 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

4 回答 4

1

像这样使用

View v = super.getView(position, convertView, parent);
   v.setBackgroundColor(Color.YELLOW);
于 2013-05-29T05:10:54.903 回答
1

如果你查看堆栈跟踪,没有提到 setColor,它在一些内部布局相关的函数中崩溃。
那是因为自从您覆盖 getView() 以来,视图永远不会被创建。
首先在 getView() 中调用 super.getView() 然后修改并返回其结果。

编辑:虽然您通常会覆盖 getView() 来增加自定义布局,但原始海报只想更改背景颜色。调用 super.getView(...) 将使 Android 的实现膨胀布局,然后他可以做他的小调整。更少的原始代码,更少的原始错误:)

于 2013-05-29T05:19:46.300 回答
0

在您的 color.xml 文件中创建绿色值并按以下方式使用它

v.setBackgroundResource(R.color.green);
于 2013-05-29T05:24:40.603 回答
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
}
于 2013-05-29T06:58:39.820 回答