gpt4 book ai didi

android - 我可以改变许多开关

转载 作者:行者123 更新时间:2023-11-30 02:35:54 26 4
gpt4 key购买 nike

我正在制作我的第一个 Android 应用。我正在使用 ArrayAdapter 类来显示传感器列表及其带有开关的状态,但我不知道为什么我只能将文本设置为一个开关!其他的不改变,但我可以改变 TextView。

错误:

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);
}
}

最佳答案

我认为,您忘记了在 row 上调用 findViewById。尝试使用:row.findViewById(R.id.monitored_switch);

关于android - 我可以改变许多开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26565652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com