android - ListView returning null using Fragment -
when try retrieve listview coponent in xml layout, nullpointerexception launched.
listview lv = (listview) view.findviewbyid(r.id.lvalerts);
i'm using fragments, welcome.
1) layout file (tab_frag_alerts.xml)
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <spinner android:id="@+id/spinneralertcategory" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <listview android:id="@+id/lvalerts" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </linearlayout>
2) layout file (alert_row.xml)
<?xml version="1.0" encoding="utf-8"?> <tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dip" > <tablerow android:id="@+id/tablerow1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="1dip" > <textview android:id="@+id/alertid" style="@style/vehicledefaultfont.plate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/label_text_view" /> <textview android:id="@+id/createdin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/label_text_view" /> </tablerow> </tablelayout>
3) fragment java class:
package br.com.log2br.tabs; import android.database.cursor; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.widget.simplecursoradapter; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.adapterview.onitemselectedlistener; import android.widget.arrayadapter; import android.widget.listview; import android.widget.spinner; import br.com.log2br.r; import br.com.log2br.db.alertdbadapter; public class alertstabfragment extends fragment { string tag = getclass().getname(); private alertdbadapter dbhelper; private simplecursoradapter dataadapter; private spinner spinneralertcategory; view view; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.i(tag, "loading oncreateview()"); view = inflater.inflate(r.layout.tab_frag_alerts, container, false); dbhelper = new alertdbadapter(getactivity()); dbhelper.open(); dbhelper.deleteallalerts(); dbhelper.insertalerts(); spinneralertcategory = (spinner) view.findviewbyid(r.id.spinneralertcategory); arrayadapter<charsequence> adapter = arrayadapter.createfromresource(view.getcontext(), r.array.alerts_array, android.r.layout.simple_spinner_item); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinneralertcategory.setadapter(adapter); spinneralertcategory.setonitemselectedlistener(new onitemselectedlistener() { public void onitemselected(adapterview<?> parent, view view, int pos, long id) { string itemselected = parent.getitematposition(pos).tostring(); if(!itemselected.equals(r.string.text_hint_alert_spinner)) { cursor cursor = dbhelper.fetchallalerts(); if(cursor != null && cursor.getcount() > 0) { string[] columns = new string[] { alertdbadapter.key_alert_id, alertdbadapter.key_created_in, }; int[] = new int[] { r.id.alertid, r.id.createdin, }; dataadapter = new simplecursoradapter( view.getcontext(), r.layout.alert_row, cursor, columns, to, 0); listview lv = (listview) view.findviewbyid(r.id.lvalerts); lv.setadapter(dataadapter); //null pointer here :(( } else { log.i(tag, "found no alerts."); } } } @override public void onnothingselected(adapterview<?> arg0) { // todo auto-generated method stub } }); return view; } }
4) logcat
07-21 12:37:09.044: e/androidruntime(8393): fatal exception: main 07-21 12:37:09.044: e/androidruntime(8393): java.lang.nullpointerexception 07-21 12:37:09.044: e/androidruntime(8393): @ br.com.log2br.tabs.alertstabfragment$1.onitemselected(alertstabfragment.java:119) 07-21 12:37:09.044: e/androidruntime(8393): @ android.widget.adapterview.fireonselected(adapterview.java:892) 07-21 12:37:09.044: e/androidruntime(8393): @ android.widget.adapterview.access$200(adapterview.java:49) 07-21 12:37:09.044: e/androidruntime(8393): @ android.widget.adapterview$selectionnotifier.run(adapterview.java:860) 07-21 12:37:09.044: e/androidruntime(8393): @ android.os.handler.handlecallback(handler.java:615) 07-21 12:37:09.044: e/androidruntime(8393): @ android.os.handler.dispatchmessage(handler.java:92) 07-21 12:37:09.044: e/androidruntime(8393): @ android.os.looper.loop(looper.java:137) 07-21 12:37:09.044: e/androidruntime(8393): @ android.app.activitythread.main(activitythread.java:4745) 07-21 12:37:09.044: e/androidruntime(8393): @ java.lang.reflect.method.invokenative(native method) 07-21 12:37:09.044: e/androidruntime(8393): @ java.lang.reflect.method.invoke(method.java:511) 07-21 12:37:09.044: e/androidruntime(8393): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) 07-21 12:37:09.044: e/androidruntime(8393): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 07-21 12:37:09.044: e/androidruntime(8393): @ dalvik.system.nativestart.main(native method) 07-21 12:47:18.904: e/trace(9446): error opening trace file: no such file or directory (2)
my cursor contains _id field. doublechecked cursor , has 4 rows.
this happening because using wrong view variable find listview
.
listview lv = (listview) view.findviewbyid(r.id.lvalerts);
the view variable here refers to:
public void onitemselected(adapterview<?> parent, view view, int pos, long id) {
you need refer view
:
view = inflater.inflate(r.layout.tab_frag_alerts, container, false);
you should change declaration:
view view; //change view other name
or,
public void onitemselected(adapterview<?> parent, view someotherview, int pos, long id) {
Comments
Post a Comment