android - Custom Adapter:Row Inflated:findViewById returning null -
i making simple app reads json data , displays in listview. i've created custom adapter class named myadapter.java. inflate row findviewbyid returning null (pointed out in comment).i plan add more wigets row.xml testing have added textview. help?
row.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="6dp"> <textview android:id="@+id/tvnew_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textstyle="italic"/> </linearlayout>
myadapter.java
package com.test.rajat.contacts; import android.content.context; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.textview; import java.util.arraylist; import java.util.hashmap; public class myadapter extends arrayadapter<hashmap<string,string>> { private arraylist<hashmap<string,string>> movies_list; private context context; private textview title; public myadapter(context context, int resource,arraylist<hashmap<string,string>> movies_list) { super(context, resource,movies_list); this.context=context; this.movies_list=movies_list; } @override public view getview(int position, view convertview, viewgroup parent) { view v=convertview; if(v==null){ layoutinflater inflater=(layoutinflater) context.getsystemservice(context.layout_inflater_service); inflater.inflate(r.layout.row,null); log.d("ir","row inflated"); } hashmap<string,string> movie=movies_list.get(position); if(movie!=null){ /********this line returns null on findviewbyid********/ title=(textview)v.findviewbyid(r.id.tvnew_title); title.settext(movie.get("title")); } return v; } }
mainactivity.java
package com.test.rajat.contacts; import android.app.searchmanager; import android.content.context; import android.content.intent; import android.os.asynctask; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.support.v7.widget.searchview; import android.view.menu; import android.view.menuitem; import android.widget.listview; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.util.arraylist; import java.util.hashmap; public class mainactivity extends actionbaractivity { private listview list_movies; private myadapter adapter; private searchview searchview; jsonparser parser; arraylist<hashmap<string,string>> movies_list; private static string url="https://api.themoviedb.org/3/search/movie?api_key=<pasted key>&&query="; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); list_movies=(listview) findviewbyid(r.id.list_movies); handleintent(getintent()); parser=new jsonparser(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); searchmanager searchmanager = (searchmanager) getsystemservice(context.search_service); searchview= (searchview) menu.finditem(r.id.action_search).getactionview(); searchview.setsearchableinfo( searchmanager.getsearchableinfo(getcomponentname())); searchview.seticonifiedbydefault(false); final searchview.onquerytextlistener querytextlistener = new searchview.onquerytextlistener() { @override public boolean onquerytextchange(string newtext) { return true; } @override public boolean onquerytextsubmit(string query) { string newquery=url+query.replaceall(" ","%20"); new retrievejson().execute(newquery); searchview.clearfocus(); return true; } }; searchview.setonquerytextlistener(querytextlistener); return true; } private void handleintent(intent intent) { if (intent.action_search.equals(intent.getaction())) { string query = intent.getstringextra(searchmanager.query); //use query search data somehow } } @override protected void onnewintent(intent intent) { handleintent(intent); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement switch(id) { case r.id.action_settings: return true; case r.id.action_search: } return super.onoptionsitemselected(item); } class retrievejson extends asynctask<string,string,void> { @override protected void onpreexecute() { super.onpreexecute(); movies_list=new arraylist<hashmap<string,string>>(); } @override protected void doinbackground(string... params) { string str_json= parser.getjsonfromurl(params[0]); try { jsonobject json=new jsonobject(str_json); jsonarray jsonarray=new jsonarray(json.getstring("results")); for(int i=0;i<jsonarray.length();i++) { jsonobject tmp=jsonarray.getjsonobject(i); string title=tmp.getstring("title"); string release_date=tmp.getstring("release_date"); string overview=tmp.getstring("overview"); string rating=tmp.getstring("vote_average"); string poster_path=tmp.getstring("poster_path"); hashmap<string,string> movie=new hashmap<>(); movie.put("title",title); movie.put("release_date",release_date); movie.put("overview",overview); movie.put("rating",rating); movie.put("poster_path",poster_path); movies_list.add(movie); } } catch(jsonexception e) { } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); adapter=new myadapter(mainactivity.this,r.layout.row,movies_list); list_movies.setadapter(adapter); } } }
log file
07-14 19:31:57.182 12606-12606/com.test.rajat.contacts d/ir﹕ row inflated 07-14 19:31:57.183 12606-12606/com.test.rajat.contacts d/androidruntime﹕ shutting down vm 07-14 19:31:57.189 12606-12606/com.test.rajat.contacts e/androidruntime﹕ fatal exception: main process: com.test.rajat.contacts, pid: 12606 java.lang.nullpointerexception: attempt invoke virtual method 'android.view.view android.view.view.findviewbyid(int)' on null object reference @ com.test.rajat.contacts.myadapter.getview(myadapter.java:40) @ android.widget.abslistview.obtainview(abslistview.java:2349) @ android.widget.listview.onmeasure(listview.java:1154) @ android.view.view.measure(view.java:17554) @ android.widget.scrollview.measurechildwithmargins(scrollview.java:1260) @ android.widget.framelayout.onmeasure(framelayout.java:436) @ android.widget.scrollview.onmeasure(scrollview.java:337) @ android.view.view.measure(view.java:17554) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5536) @ android.widget.framelayout.onmeasure(framelayout.java:436) @ android.view.view.measure(view.java:17554) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5536) @ android.support.v7.internal.widget.actionbaroverlaylayout.onmeasure(actionbaroverlaylayout.java:453) @ android.view.view.measure(view.java:17554) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5536) @ android.widget.framelayout.onmeasure(framelayout.java:436) @ android.view.view.measure(view.java:17554) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5536) @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1436) @ android.widget.linearlayout.measurevertical(linearlayout.java:722) @ android.widget.linearlayout.onmeasure(linearlayout.java:613) @ android.view.view.measure(view.java:17554) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5536) @ android.widget.framelayout.onmeasure(framelayout.java:436) @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2871) @ android.view.view.measure(view.java:17554) @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:2015) @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1173) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1379) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1061) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:5891) @ android.view.choreographer$callbackrecord.run(choreographer.java:767) @ android.view.choreographer.docallbacks(choreographer.java:580) @ android.view.choreographer.doframe(choreographer.java:550) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:753) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5293) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:904) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:699)
changing
inflater.inflate(r.layout.row,null);
to
v= inflater.inflate(r.layout.row,null);
should solve problem
Comments
Post a Comment