Android How to call newInstance Constructor -
i want display slideshow using viewpager
imageview
, textview
. when called fragment.newinstance
(int image,string value) constructor shows error below.
did called constructor correctly or not?
error:
06-09 15:26:17.746: e/androidruntime(14955): fatal exception: main 06-09 15:26:17.746: e/androidruntime(14955): process: com.example.launchactivity, pid: 14955 06-09 15:26:17.746: e/androidruntime(14955): java.lang.nullpointerexception 06-09 15:26:17.746: e/androidruntime(14955): @ com.example.launchactivity.launchsliderfragment.newinstance(launchsliderfragment.java:33) 06-09 15:26:17.746: e/androidruntime(14955): @ com.example.launchactivity.launchactivity$screenslidepageradapter.getitem(launchactivity.java:44) 06-09 15:26:17.746: e/androidruntime(14955): @ android.support.v4.app.fragmentstatepageradapter.instantiateitem(fragmentstatepageradapter.java:105) 06-09 15:26:17.746: e/androidruntime(14955): @ android.support.v4.view.viewpager.addnewitem(viewpager.java:832) 06-09 15:26:17.746: e/androidruntime(14955): @ android.support.v4.view.viewpager.populate(viewpager.java:982) 06-09 15:26:17.746: e/androidruntime(14955): @ android.support.v4.view.viewpager.populate(viewpager.java:914) 06-09 15:26:17.746: e/androidruntime(14955): @ android.support.v4.view.viewpager.onmeasure(viewpager.java:1436) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.view.measure(view.java:17495) 06-09 15:26:17.746: e/androidruntime(14955): @ android.widget.linearlayout.measurevertical(linearlayout.java:847) 06-09 15:26:17.746: e/androidruntime(14955): @ android.widget.linearlayout.onmeasure(linearlayout.java:588) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.view.measure(view.java:17495) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5363) 06-09 15:26:17.746: e/androidruntime(14955): @ android.widget.framelayout.onmeasure(framelayout.java:310) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.view.measure(view.java:17495) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5363) 06-09 15:26:17.746: e/androidruntime(14955): @ android.widget.framelayout.onmeasure(framelayout.java:310) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.view.measure(view.java:17495) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5363) 06-09 15:26:17.746: e/androidruntime(14955): @ com.android.internal.widget.actionbaroverlaylayout.onmeasure(actionbaroverlaylayout.java:412) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.view.measure(view.java:17495) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5363) 06-09 15:26:17.746: e/androidruntime(14955): @ android.widget.framelayout.onmeasure(framelayout.java:310) 06-09 15:26:17.746: e/androidruntime(14955): @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2548) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.view.measure(view.java:17495) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:2285) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1396) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1595) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1254) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:6637) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.choreographer$callbackrecord.run(choreographer.java:814) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.choreographer.docallbacks(choreographer.java:614) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.choreographer.doframe(choreographer.java:584) 06-09 15:26:17.746: e/androidruntime(14955): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:800) 06-09 15:26:17.746: e/androidruntime(14955): @ android.os.handler.handlecallback(handler.java:733) 06-09 15:26:17.746: e/androidruntime(14955): @ android.os.handler.dispatchmessage(handler.java:95) 06-09 15:26:17.746: e/androidruntime(14955): @ android.os.looper.loop(looper.java:146) 06-09 15:26:17.746: e/androidruntime(14955): @ android.app.activitythread.main(activitythread.java:5602) 06-09 15:26:17.746: e/androidruntime(14955): @ java.lang.reflect.method.invokenative(native method) 06-09 15:26:17.746: e/androidruntime(14955): @ java.lang.reflect.method.invoke(method.java:515) 06-09 15:26:17.746: e/androidruntime(14955): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1283) 06-09 15:26:17.746: e/androidruntime(14955): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1099) 06-09 15:26:17.746: e/androidruntime(14955): @ dalvik.system.nativestart.main(native method)
activity:
public class launchactivity extends fragmentactivity { private static final int num_pages = 4; private viewpager mpager; private pageradapter mpageradapter; string[] text={"property showcase","bill payment","golf virtual tour","news , events"}; int[] image={r.drawable.slide1,r.drawable.slide2,r.drawable.slide3,r.drawable.slide4}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_launch); mpager = (viewpager) findviewbyid(r.id.pager); mpageradapter = new screenslidepageradapter(getsupportfragmentmanager()); mpager.setadapter(mpageradapter); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.launch, menu); return true; } private class screenslidepageradapter extends fragmentstatepageradapter { public screenslidepageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { new launchsliderfragment(); return launchsliderfragment.newinstance(image[position], text[position]); } @override public int getcount() { return num_pages; } } }
fragment:
public class launchsliderfragment extends fragment { private imageview imageview; private textview textview; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // todo auto-generated method stub view view = inflater.inflate(r.layout.launch_slider_fragment, null); imageview = (imageview) view.findviewbyid(r.id.launch_sliderimage); textview = (textview) view.findviewbyid(r.id.launch_slidertext); return view; } public static launchsliderfragment newinstance(int imageid, string string) { launchsliderfragment launchsliderfragment = new launchsliderfragment(); bundle args = new bundle(); args.putint("image", imageid); args.putstring("text", string); launchsliderfragment.setarguments(args); launchsliderfragment.imageview.setimageresource(imageid); launchsliderfragment.textview.settext(string); return launchsliderfragment; } public launchsliderfragment() { // todo auto-generated constructor stub } }
Comments
Post a Comment