android - can't start activity ,caused maybe by thread/handler/adapter/listview,not for sure -


i'm writing book-searching app on android,something wrong code application force closed when click button start search-activity.from log,i think maybe use handler or thread or adapter incorrectly,but can't figure out.need help,thanks.

activity file:

package com.example.doubanbook.activity;  import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map;  import android.app.activity; import android.graphics.bitmap; import android.os.bundle; import android.os.handler; import android.os.message; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.imageview; import android.widget.listview; import android.widget.simpleadapter; import android.widget.simpleadapter.viewbinder;  import com.example.doubanbook.r; import com.example.doubanbook.book.booksearchinfo; import com.example.doubanbook.book.pasesearchreasult; import com.example.doubanbook.book.util;  public class searchbookactivity extends activity {      private edittext searchedittext = null;     private button searchbutton = null;     private listview lv = null;     private handler hd ;     private list<map<string, object>> listitems = new arraylist<map<string, object>>();     private simpleadapter adapt = null;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.searchbook);          searchedittext = (edittext) findviewbyid(r.id.search_et);         searchbutton = (button) findviewbyid(r.id.search_button);         lv = (listview) findviewbyid(r.id.lv);         searchbutton.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 string keystr = searchedittext.gettext().tostring();                  new downloadthread("https://api.douban.com/v2/book/search?q="                         + keystr).start();              }         });         hd = new handler() {             @override             public void handlemessage(message msg) {                 super.handlemessage(msg);                  arraylist<booksearchinfo> infos = (arraylist<booksearchinfo>) msg.obj;                 (int = 0; < infos.size(); i++) {                     map<string, object> listitem = new hashmap<string, object>();                     listitem.put("title", infos.get(i).gettitle());                     listitem.put("author", infos.get(i).getauthor());                     listitem.put("isbn", infos.get(i).getisbn());                     listitem.put("publisher", infos.get(i).getpublisher());                     listitem.put("rate", infos.get(i).getrate());                     listitem.put("image", infos.get(i).getcover());                     listitems.add(listitem);                 }                 adapt = new simpleadapter(searchbookactivity.this, listitems,                         r.layout.search_listitem,                         new string[] { "title", "author", "isbn", "publisher",                                 "rate", "image" }, new int[] { r.id.tv1,                                 r.id.tv2, r.id.tv3, r.id.tv4, r.id.iv });                 adapt.setviewbinder(new viewbinder() {                      @override                     public boolean setviewvalue(view view, object data,                             string textrepresentation) {                         if ((view instanceof imageview)                                 & (data instanceof bitmap)) {                             imageview iv = (imageview) view;                             bitmap bm = (bitmap) data;                             iv.setimagebitmap(bm);                             return true;                         }                         return false;                     }                 });                 lv.setadapter(adapt);             }          };     }     class downloadthread extends thread {         string url = null;          public downloadthread(string urlstr) {             url = urlstr;         }          public void run() {             string result = util.download(url);             pasesearchreasult mess = new pasesearchreasult(result);             arraylist<booksearchinfo> infos = mess.resultcollection();             message msg = message.obtain();             msg.obj = infos;              hd.sendmessage(msg);          }     } } 

layout xml file:

<?xml version="1.0" encoding="utf-8"?> <scrollview xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent" >      <linearlayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="vertical" >          <textview             android:layout_width="wrap_content"             android:layout_height="match_parent"             android:layout_gravity="center"             android:gravity="center"             android:text="search" />          <linearlayout             android:layout_width="match_parent"             android:layout_height="match_parent"             android:orientation="horizontal" >              <edittext                 android:id="@+id/search_et"                 android:layout_width="0dp"                 android:layout_height="match_parent"                 android:layout_gravity="center"                 android:layout_weight="3"                 android:background="@null"                 android:gravity="center"                 android:textcolor="#ffffff"                 android:hint="type keyword" />              <button                 android:id="@+id/search_btn"                 android:layout_width="0dp"                 android:layout_height="match_parent"                 android:layout_gravity="center"                 android:text="search"                 android:layout_weight="1" />         </linearlayout>          <listview             android:id="@+id/lv"             android:layout_width="match_parent"             android:layout_height="wrap_content" >         </listview>     </linearlayout>  </scrollview> 

list item xml file:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >      <imageview         android:id="@+id/iv"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_weight="1" />      <textview         android:id="@+id/lv1"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_weight="1" />      <textview         android:id="@+id/lv2"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_weight="1" />      <textview         android:id="@+id/lv3"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_weight="1" />      <textview         android:id="@+id/lv4"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_weight="1" />   </linearlayout> 

log information:

04-14 14:33:38.269: e/openglrenderer(1263): getting max_texture_size gradiencache 04-14 14:33:38.273: e/openglrenderer(1263): max_texture_size: 16384 04-14 14:33:38.285: e/openglrenderer(1263): getting max_texture_size caches::initconstraints() 04-14 14:33:38.285: e/openglrenderer(1263): max_texture_size: 16384 04-14 14:33:43.145: e/androidruntime(1263): fatal exception: main 04-14 14:33:43.145: e/androidruntime(1263): process: com.example.doubanbook, pid: 1263 04-14 14:33:43.145: e/androidruntime(1263): java.lang.runtimeexception: unable start activity componentinfo{com.example.doubanbook/com.example.doubanbook.activity.searchbookactivity}: java.lang.nullpointerexception 04-14 14:33:43.145: e/androidruntime(1263):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 04-14 14:33:43.145: e/androidruntime(1263):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 04-14 14:33:43.145: e/androidruntime(1263):     @ android.app.activitythread.access$800(activitythread.java:135) 04-14 14:33:43.145: e/androidruntime(1263):     @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 04-14 14:33:43.145: e/androidruntime(1263):     @ android.os.handler.dispatchmessage(handler.java:102) 04-14 14:33:43.145: e/androidruntime(1263):     @ android.os.looper.loop(looper.java:136) 04-14 14:33:43.145: e/androidruntime(1263):     @ android.app.activitythread.main(activitythread.java:5017) 04-14 14:33:43.145: e/androidruntime(1263):     @ java.lang.reflect.method.invokenative(native method) 04-14 14:33:43.145: e/androidruntime(1263):     @ java.lang.reflect.method.invoke(method.java:515) 04-14 14:33:43.145: e/androidruntime(1263):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 04-14 14:33:43.145: e/androidruntime(1263):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 04-14 14:33:43.145: e/androidruntime(1263):     @ dalvik.system.nativestart.main(native method) 04-14 14:33:43.145: e/androidruntime(1263): caused by: java.lang.nullpointerexception 04-14 14:33:43.145: e/androidruntime(1263):     @ com.example.doubanbook.activity.searchbookactivity.oncreate(searchbookactivity.java:45) 04-14 14:33:43.145: e/androidruntime(1263):     @ android.app.activity.performcreate(activity.java:5231) 04-14 14:33:43.145: e/androidruntime(1263):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 04-14 14:33:43.145: e/androidruntime(1263):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 04-14 14:33:43.145: e/androidruntime(1263):     ... 11 more 

the id wrong here:

searchbutton = (button) findviewbyid(r.id.search_button); 

because in layout.xml put different id:

android:id="@+id/search_btn" 

==> use same id @ layout , activity , should work, otherwise findviewbyid return null because not find id.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -