从自定义适配器更改ListView中的形状背景颜色

时间:2014-01-05 17:14:09

标签: android listview

我有一个ListView,它有一些人和他们独特的颜色,我用xml创建了一个矩形,然后把它作为ImageView的背景。当我试图改变它的颜色时它会崩溃。

这是我的自定义适配器的getView()方法:

  vi = inflater.inflate(R.layout.sonuclisterow, null);
  ImageView adayRenk = (ImageView)vi.findViewById(R.id.adayRenk);
  GradientDrawable sd = (GradientDrawable) adayRenk.getBackground();
  sd.setColor(Color.BLACK); //Color.BLACK is just for trying to change, there will be variable later

形状:

 <?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
   <solid android:color="#F0F0F0" />
</shape> 

logcat的:

01-05 19:11:32.545: D/dalvikvm(30161): GC_FOR_ALLOC freed 5735K, 23% free 20374K/26183K, paused 18ms, total 18ms
01-05 19:11:32.665: D/AndroidRuntime(30161): Shutting down VM
01-05 19:11:32.665: W/dalvikvm(30161): threadid=1: thread exiting with uncaught exception (group=0x40f24300)
01-05 19:11:32.675: E/AndroidRuntime(30161): FATAL EXCEPTION: main
01-05 19:11:32.675: E/AndroidRuntime(30161): java.lang.NullPointerException
01-05 19:11:32.675: E/AndroidRuntime(30161):    at com.paperless.custom.LazyAdapter2.getView(LazyAdapter2.java:63)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.widget.AbsListView.obtainView(AbsListView.java:2267)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.widget.ListView.onMeasure(ListView.java:1156)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.View.measure(View.java:15172)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.View.measure(View.java:15172)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.View.measure(View.java:15172)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.View.measure(View.java:15172)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.View.measure(View.java:15172)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.View.measure(View.java:15172)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:736)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.Choreographer.doCallbacks(Choreographer.java:566)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.Choreographer.doFrame(Choreographer.java:536)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:722)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.os.Handler.handleCallback(Handler.java:615)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.os.Looper.loop(Looper.java:137)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at android.app.ActivityThread.main(ActivityThread.java:4745)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at java.lang.reflect.Method.invokeNative(Native Method)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at java.lang.reflect.Method.invoke(Method.java:511)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-05 19:11:32.675: E/AndroidRuntime(30161):    at dalvik.system.NativeStart.main(Native Method)

LazyAdapter2上的第63行是sd.setColor(Color.BLACK);

1 个答案:

答案 0 :(得分:1)

必须使用ImageView的{​​{3}}属性设置背景。