我尝试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)