-2

我是 android 新手,在我的应用程序中,我想更改自定义列表视图的颜色。这是我的代码

String[] from = new String[] { Contactsnew.CONTENT};
int[] to = new int[] { R.id.content123, R.id.linear123};
custAdapter = new SimpleCursorAdapter(this,
        R.layout.activity_contactitem, null, from, to);
//ListView listView = (ListView) findViewById(R.id.listv);
listview.setAdapter(custAdapter);
    custAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){

    @Override
    public boolean setViewValue(View view, Cursor cursor, int col) {
        // TODO Auto-generated method stub
        Log.d("asda", "asd");
        if(view.getId() == R.id.linear123){
        RelativeLayout relative = (RelativeLayout)view.findViewById(R.id.linear123);
        relative.setBackgroundColor(0xFFC1E7F);
        return true;
        }
        return false;
    }

});

运行后显示 ArrayIndexoutofbounds。

日志报告

08-13 12:44:57.004: E/AndroidRuntime(2107): FATAL EXCEPTION: main 
08-13 12:44:57.004: E/AndroidRuntime(2107): java.lang.ArrayIndexOutOfBoundsException
08-13 12:44:57.004: E/AndroidRuntime(2107):     at        android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:131)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.ListView.fillDown(ListView.java:652)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.ListView.fillFromTop(ListView.java:709)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.ListView.layoutChildren(ListView.java:1566)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.view.View.layout(View.java:7035)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.view.View.layout(View.java:7035)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.ScrollView.onLayout(ScrollView.java:1205)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.view.View.layout(View.java:7035)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at  android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.view.View.layout(View.java:7035)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at android.view.View.layout(View.java:7035)
08-13 12:44:57.004: E/AndroidRuntime(2107):     at 

像这样我得到。

4

2 回答 2

0

你在错误的视图上运行命令findViewById,你所做的是在列表视图中找到相对布局(当然不会在那里找到它,这就是你得到null的原因)。
findViewById 仅在激活的对象下搜索视图(在您的情况下为列表视图)。

您需要在findViewById包含RelativeLayout.

如果您在片段中运行,请尝试getView().findViewById(...)

于 2013-08-13T12:16:23.510 回答
0

那是因为变量“相对”为空。您是否设置了当前视图?如果不是 -> 通过 ID 获取视图时,您将获得 NPE

于 2013-08-13T11:51:57.297 回答