android - onCreate Null Pointer Exception -
this question has answer here:
i don't understand why code not running:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_matchpage); seekbar sb1 = (seekbar) findviewbyid(r.id.seek1); sb1.setenabled(false); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } }
sb1 null. why?
you using fragment
. should have in code class called placeholderfragment
, has oncreateview
method.
in project have 2 xml layout, 1 named activity_matchpage
, other fragment, fragment_main
. need modify latter , keep first this:
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="youor.packaque.mainactivity" tools:ignore="mergerootframe" />
and in fragment_main
, declare views.
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.tformacion.finale.mainactivity$placeholderfragment" > <!-- views here --> </relativelayout>
then, in java code, in placeholderfragment
, inside of oncreateview
:
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); seekbar = (seekbar) rootview.findviewbyid(r.id.yourseekbarid); }
if want rid of fragment create xml in activity_matchpage
, not add fragment, so, remove this:
if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); }
Comments
Post a Comment