Android Application Crashing When Switching Activities -
upon clicking item of listview want new activity start up. when run through emulator application crashes every time. positive androidmanifest correct well. appreciated.
public static class sectionfragment extends listfragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; private string dataarrayone[]; private string dataarraytwo[]; public sectionfragment() { dataarrayone = new string[] { "steven's portfolio", "sean's portfolio", "logan's portfolio", }; dataarraytwo = new string[] { "goog", "yhoo", "aapl", "msft" }; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); switch(getarguments().getint(arg_section_number)) { case 0: listadapter listadapter = new arrayadapter<string>(getactivity(), android.r.layout.simple_list_item_1, dataarrayone); setlistadapter(listadapter); break; case 1: listadapter listadapter1 = new arrayadapter<string>(getactivity(), android.r.layout.simple_list_item_1, dataarraytwo); setlistadapter(listadapter1); } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { return inflater.inflate(r.layout.fragment_main_dummy, container, false); } @override public void onlistitemclick(listview list, view v, int position, long id) { toast.maketext(getactivity(), getlistview().getitematposition(position).tostring(), toast.length_short).show(); intent intent = new intent(getactivity(), portfolio_select.class); intent.putextra("username", getlistview().getitematposition(position).tostring()); startactivity(intent); }
androidmanifest.xml
<activity android:name=".portfolio_select" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.default" /> </intent-filter> </activity>
portfolio_select code
public class portfolio_select extends activity { // variables go here textview display; /** called when activity first created. */ @override public void oncreate(final bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(com.example.stockticker.r.layout.activity_main); display.settext("stocks go here!"); } }
textview display;
is declared, references null
since findviewbyid()
hasn't been used give else hold.
this means when
display.settext("stocks go here!");
is called, you'll npe.
so use make sure have textview
in r.layout.activity_main
id, use findviewbyid()
give display
textview
reference
setcontentview(com.example.stockticker.r.layout.activity_main); display = (textview) findviewbyid (r.id.your_text_view_id); display.settext("stocks go here!");
if there other issues, add stack trace question, there's no other guessing involved.
Comments
Post a Comment