在我的程序中,只要我创建一个ListView并将Simpleadapter设置为适配器,我就会尝试访问此listView中的View,以便根据条件更改此视图的背景。我用方法ListView.getChildAt(position)来做。但是,我得到一个nullPointer异常,我不明白为什么。这是我的代码的一部分。 为了更好地理解下面的代码:我实际上在代码中创建了2个listViews,Alarm是我实现的类。我只是通过这个课程检索一些信息。
Java代码:
public class SmartAlarm extends AppCompatActivity {
private ListView list_view_alarms;
private ListView list_view_activates;
private List<HashMap<String, String>> listMapOfEachAlarm;
private List<HashMap<String, Integer>> listMapOfActivates;
private SimpleAdapter adapter_alarms;
private SimpleAdapter adapter_activates;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smart_alarm);
list_view_alarms = (ListView) findViewById(R.id.list_alarm);
list_view_activates = (ListView) findViewById(R.id.list_activate);
listMapOfEachAlarm = new ArrayList<>();
listMapOfActivates = new ArrayList<>();
adapter_alarms = new SimpleAdapter(this, listMapOfEachAlarm, R.layout.item_alarm,
new String[]{"alarm", "title"}, new int[]{R.id.time, R.id.title});
adapter_activates = new SimpleAdapter(this, listMapOfActivates, R.layout.item_activate, new String[]{"alarm_drawable"}, new int[]{R.id.activate});
for (Alarm alarm : alarmList) {
HashMap<String, String> mapOfTheNewAlarm = new HashMap<>();
mapOfTheNewAlarm.put("alarm", alarm.getTime());
mapOfTheNewAlarm.put("title", alarm.getTitle());
listMapOfEachAlarm.add(mapOfTheNewAlarm);
HashMap<String, Integer> mapOfTheAlarmDrawable = new HashMap<>();
if (alarm.getActivated()) {
mapOfTheAlarmDrawable.put("alarm_drawable", R.drawable.alarm_on);
} else {
mapOfTheAlarmDrawable.put("alarm_drawable", R.drawable.alarm_off);
}
listMapOfActivates.add(mapOfTheAlarmDrawable);
}
list_view_alarms.setAdapter(adapter_alarms);
list_view_activates.setAdapter(adapter_activates);
for(int i=0; i<list_view_alarms.getCount();i++)
{
if(conditionRespected()){
list_view_alarms.getChildAt(i)
.setBackgroundColor(getResources().getColor (R.color.dark)); //The compilation error is here because list_view_alarms.getChildAt(i) is null
}
}
}
}
activity_smart_alarm.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/crazy_alarm"
android:orientation="vertical"
android:weightSum="10">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Check the box if you want to activate the game" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/list_alarm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8"
android:choiceMode="singleChoice"
android:divider="#FF0000"
android:dividerHeight="2dp" />
<ListView
android:id="@+id/list_activate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:choiceMode="singleChoice"
android:divider="#FF0000"
android:dividerHeight="2dp" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
您没有设置主视图。您的列表视图必须在您的活动实际将使用的视图内创建。在onCreate()
添加setContentView(R.layout.activity_main);
您还必须创建一个布局(在本例中为activity_main
),并且您的listView必须是activity_main
布局的子项
这可能是您activity_main
的样子
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
//and inside of here would be your listview
<android.support.v7.widget.ListViewCompat
android:id="@+id/list_alarm"
android:layout_width=""
android:layout_height=""></android.support.v7.widget.ListViewCompat>
</RelativeLayout>
答案 1 :(得分:0)
对于感兴趣的人:要根据特定条件更改listView中每个视图的背景,我最终覆盖了方法getView(),如下所示:
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = super.getView(position, convertView, parent);
if(condition){
convertView.setBackgroundColor(getResources().getColor(R.color.dark));
}
else
{
convertView.setBackgroundColor(getResources().getColor(R.color.bright));
}
return convertView;
}