android - RuntimeException when loading image from device and applying parallax effect -


in app want load image gallery or take image using camera , want apply parallax scrollview effect on image.i have xml file called header ,it contains image view , 2 buttons,one loading image 1 taking image using camera.i tried code app crashes when run app ,it shows runtime exception in logcat,can me find out problem??

logcat

06-03 12:39:02.683  27327-27327/? e/androidruntime﹕ fatal exception: main java.lang.runtimeexception: unable start activity componentinfo{in.zoid.parallaxtutorial/in.zoid.parallaxtutorial.mainactivity}: java.lang.nullpointerexception         @ android.app.activitythread.performlaunchactivity(activitythread.java:2110)         @ android.app.activitythread.handlelaunchactivity(activitythread.java:2135)         @ android.app.activitythread.access$700(activitythread.java:140)         @ android.app.activitythread$h.handlemessage(activitythread.java:1237)         @ android.os.handler.dispatchmessage(handler.java:99)         @ android.os.looper.loop(looper.java:137)         @ android.app.activitythread.main(activitythread.java:4921)         @ java.lang.reflect.method.invokenative(native method)         @ java.lang.reflect.method.invoke(method.java:511)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1027)         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:794)         @ dalvik.system.nativestart.main(native method)  caused by: java.lang.nullpointerexception         @ in.zoid.parallaxtutorial.mainactivity.oncreate(mainactivity.java:71)         @ android.app.activity.performcreate(activity.java:5206)         @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1094)         @ android.app.activitythread.performlaunchactivity(activitythread.java:2074) 

  

mainactivity.java   public class mainactivity extends activity {  private int lasttop = 0; //imageview image; listview listview; private static int result_load_image = 1; private static final int camera_request = 1888; private imageview imageview; arrayadapter adapter; arraylist<string> items = new arraylist<>();  public void parallax(final view v) {     final rect r = new rect();     v.getlocalvisiblerect(r);      if (lasttop != r.top) {         lasttop = r.top;         v.post(new runnable() {             @override             public void run() {                 v.sety((float) (r.top / 2.0));             }         });     } }  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     listview = (listview) findviewbyid(r.id.listview);      items.add("list item 1");     items.add("list item 2");     items.add("list item 3");     items.add("list item 4");     items.add("list item 5");     items.add("list item 6");     items.add("list item 7");     items.add("list item 8");     items.add("list item 9");     items.add("list item 10");     items.add("list item 11");     items.add("list item 12");     items.add("list item 13");     items.add("list item 14");     items.add("list item 15");     items.add("list item 16");     items.add("list item 17");     items.add("list item 18");     items.add("list item 19");     items.add("list item 20");      view view = getlayoutinflater().inflate(r.layout.header, null, false);      button buttonloadimage = (button) findviewbyid(r.id.buttonloadpicture);     buttonloadimage.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view arg0) {               intent = new intent(                     intent.action_pick,                     android.provider.mediastore.images.media.external_content_uri);              startactivityforresult(i, result_load_image);         }     });     this.imageview = (imageview)this.findviewbyid(r.id.imgview);    /* button photobutton = (button) this.findviewbyid(r.id.button2);     photobutton.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view v) {             intent cameraintent = new intent(android.provider.mediastore.action_image_capture);             startactivityforresult(cameraintent, camera_request);         }     });*/      //imageview block end       // image = (imageview) view.findviewbyid(r.id.image);     listview.addheaderview(view);      adapter = new arrayadapter(this, android.r.layout.simple_list_item_1, items);     listview.setadapter(adapter);     adapter.notifydatasetchanged();     listview.setonscrolllistener(new abslistview.onscrolllistener() {         @override         public void onscrollstatechanged(abslistview view, int scrollstate) {             parallax(imageview);         }          @override         public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {             parallax(imageview);         }     }); } } 

header.xml

<!--?xml version="1.0" encoding="utf-8"?--> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <imageview android:id="@+id/imgview"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_weight="1"></imageview> <relativelayout     android:id="@+id/relativelayout1"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:background="@android:color/black" >     <button android:id="@+id/buttonloadpicture"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_weight="0"         android:text="load picture"         android:layout_gravity="center"></button>     <button         android:id="@+id/button2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="take picture"         android:layout_alignparentright="true"/> </relativelayout> </linearlayout> 

i think problem here:

button buttonloadimage = (button) findviewbyid(r.id.buttonloadpicture); 

you should use this

button buttonloadimage = (button) view.findviewbyid(r.id.buttonloadpicture); 

because header not added activity layout yet, code null pointer when find buttonloadpicture.


Comments