Android 我只需要换一个开关就可以了
我正在用Android制作我的第一个应用程序。我正在使用ArrayAdapter类通过一个开关显示传感器列表及其状态,但我不知道为什么我可以将文本设置为一个开关!其他的没有改变,但是我可以改变文本视图 错误:Android 我只需要换一个开关就可以了,android,Android,我正在用Android制作我的第一个应用程序。我正在使用ArrayAdapter类通过一个开关显示传感器列表及其状态,但我不知道为什么我可以将文本设置为一个开关!其他的没有改变,但是我可以改变文本视图 错误: a:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setText(java.lang.CharSequence)' on a null object
a:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setText(java.lang.CharSequence)' on a null object reference
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/descripcion"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="@+id/TableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<!-- >TextView
android:id="@+id/descripcion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /-->
<Switch
android:id="@+id/monitored_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textOff="Off"
android:textOn="On" />
</TableRow>
<TableRow
android:id="@+id/TableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/urli"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</TableRow>
</TableLayout>
这是我的ArrayAdapter课程:
class ListaAdapter extends ArrayAdapter<JSONObject> {
ListaAdapter() {
super(MainActivity.this, R.layout.itemlist,data);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.itemlist, parent, false);
}
try {
// TextView name_lbl = (TextView) row.findViewById(R.id.nombre);
// name_lbl.setText(data.get(position).getString("name"));
TextView desc_lbl = (TextView) row.findViewById(R.id.descripcion);
desc_lbl.setText(data.get(position).getString("description"));
Switch s = (Switch) findViewById(R.id.monitored_switch);
// s.setChecked(true);
s.setText("fdsdf"+data.get(position).getString("id"));
if (s != null) {
// s.setOnCheckedChangeListener(this);
}
// TextView url_lbl = (TextView) row.findViewById(R.id.urli);
// url_lbl.setText(data.get(position).getString("url"));
}
catch (Exception e) {
Log.e(ListaAdapter.class.getName(),data.get(position)+"Fallo al rellenar la lista:" + e.toString());
}
return (row);
}
}
类ListaAdapter扩展了ArrayAdapter{
ListaAdapter(){
super(MainActivity.this,R.layout.itemlist,data);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
if(行==null){
LayoutInflater充气机=getLayoutInflater();
行=充气机。充气(R.layout.itemlist,父项,false);
}
试一试{
//TextView name_lbl=(TextView)row.findViewById(R.id.nombre);
//name_lbl.setText(data.get(position).getString(“name”);
TextView desc_lbl=(TextView)row.findViewById(R.id.description);
desc_lbl.setText(data.get(position).getString(“description”);
开关s=(开关)findViewById(R.id.U开关);
//s.setChecked(真);
s、 setText(“fdsdf”+data.get(position.getString(“id”);
如果(s!=null){
//s.setOnCheckedChangeListener(本);
}
//TextView url_lbl=(TextView)row.findViewById(R.id.urli);
//url_lbl.setText(data.get(position).getString(“url”);
}
捕获(例外e){
Log.e(ListaAdapter.class.getName(),data.get(position)+“Fallo al rellenar la lista:”+e.toString());
}
返回(行);
}
}
我认为,您忘记调用行上的findViewById
。尝试使用:row.findviewbyd(R.id.monitored\u开关)代码>是的,这就是问题所在。我觉得自己很愚蠢!非常感谢@vzoha