android - java.lang.NullPointerException with SeekBar and MediaPlayer -


i'm new in android , i'm trying run seekbar while playing audio.

i'm getting error "unable start activity componentinfo: java.lang.nullpointerexception" when try set range of progress bar "setmax".

this code:

public class playeractivity extends activity {     private mediaplayer mplayer;     private seekbar mseekbar;       @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_player);          if (savedinstancestate == null) {             getfragmentmanager().begintransaction()                     .add(r.id.container, new placeholderfragment()).commit();         }          initvariables();     }      public void initvariables(){         mplayer = new mediaplayer();         mplayer = mediaplayer.create(this, r.raw.raunchy);         mplayer.setoncompletionlistener(oncompletion);          mseekbar = (seekbar) findviewbyid(r.id.seekbar1);         mseekbar.setmax(mplayer.getduration());     }      private mediaplayer.oncompletionlistener oncompletion = new mediaplayer.oncompletionlistener() {         @override         public void oncompletion(mediaplayer mplayer) {             mplayer.release();             log.d("final", "correcto");         }     };      public void playorpause(view v) {         if (!mplayer.isplaying()) {             mplayer.start();         } else {             mplayer.pause();         }         mhandler.postdelayed(mrunnable, 100);     }      private handler mhandler = new handler();     private runnable mrunnable = new runnable() {         @override         public void run() {             mseekbar.setmax(mplayer.getduration());             if(mplayer.isplaying()){                 int mcurrentposition = mplayer.getcurrentposition() / 1000;                 mseekbar.setprogress(mcurrentposition);             }             mhandler.postdelayed(this, 1000);         }     };      @override     public boolean oncreateoptionsmenu(menu menu) {          // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();         if (id == r.id.action_settings) {             return true;         }         return super.onoptionsitemselected(item);     }      /**      * placeholder fragment containing simple view.      */     public static class placeholderfragment extends fragment {          public placeholderfragment() {         }          @override         public view oncreateview(layoutinflater inflater, viewgroup container,                 bundle savedinstancestate) {             view rootview = inflater.inflate(r.layout.fragment_player,                     container, false);             return rootview;         }     }  } 

this fragment.

<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.jgutierrezgil.bmusic.mainactivity$placeholderfragment" >  <button     android:id="@+id/button_play"     style="?android:attr/buttonstylesmall"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparentbottom="true"     android:layout_centerhorizontal="true"     android:text="@string/play_button"     android:onclick="playorpause" />  <button     android:id="@+id/button_stop"     style="?android:attr/buttonstylesmall"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparentbottom="true"     android:layout_torightof="@+id/button_play"     android:text="@string/stop_button"     android:onclick="stop" />  <button     android:id="@+id/button1"     style="?android:attr/buttonstylesmall"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignparentbottom="true"     android:layout_toleftof="@+id/button_play"     android:text="button" />  <seekbar     android:id="@+id/seekbar1"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_above="@+id/button_play"     android:layout_centerhorizontal="true"     android:layout_marginbottom="14dp" />  <textview     android:id="@+id/starttimefield"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_above="@+id/button1"     android:layout_alignleft="@+id/seekbar1"     android:text="small text"     android:textappearance="?android:attr/textappearancesmall" />  <textview     android:id="@+id/endtimefield"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_above="@+id/button_stop"     android:layout_alignright="@+id/seekbar1"     android:text="small text"     android:textappearance="?android:attr/textappearancesmall" />  <button     android:id="@+id/library"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignbaseline="@+id/player"     android:layout_alignbottom="@+id/player"     android:layout_alignright="@+id/seekbar1"     android:layout_torightof="@+id/button_play"     android:text="@string/library_button"     android:onclick="tolibrary" />  <button     android:id="@+id/player"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignleft="@+id/seekbar1"     android:layout_alignparenttop="true"     android:layout_toleftof="@+id/button_play"     android:clickable="false"     android:focusable="false"     android:longclickable="false"     android:text="@string/player_button" /> 

and logcat.

    06-03 22:04:47.023: e/androidruntime(1191): fatal exception: main 06-03 22:04:47.023: e/androidruntime(1191): process: com.jgutierrezgil.bmusic, pid: 1191 06-03 22:04:47.023: e/androidruntime(1191): java.lang.runtimeexception: unable start activity componentinfo{com.jgutierrezgil.bmusic/com.jgutierrezgil.bmusic.playeractivity}: java.lang.nullpointerexception 06-03 22:04:47.023: e/androidruntime(1191):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 06-03 22:04:47.023: e/androidruntime(1191):     @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 06-03 22:04:47.023: e/androidruntime(1191):     @ android.app.activitythread.access$800(activitythread.java:135) 06-03 22:04:47.023: e/androidruntime(1191):     @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 06-03 22:04:47.023: e/androidruntime(1191):     @ android.os.handler.dispatchmessage(handler.java:102) 06-03 22:04:47.023: e/androidruntime(1191):     @ android.os.looper.loop(looper.java:136) 06-03 22:04:47.023: e/androidruntime(1191):     @ android.app.activitythread.main(activitythread.java:5017) 06-03 22:04:47.023: e/androidruntime(1191):     @ java.lang.reflect.method.invokenative(native method) 06-03 22:04:47.023: e/androidruntime(1191):     @ java.lang.reflect.method.invoke(method.java:515) 06-03 22:04:47.023: e/androidruntime(1191):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 06-03 22:04:47.023: e/androidruntime(1191):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 06-03 22:04:47.023: e/androidruntime(1191):     @ dalvik.system.nativestart.main(native method) 06-03 22:04:47.023: e/androidruntime(1191): caused by: java.lang.nullpointerexception 06-03 22:04:47.023: e/androidruntime(1191):     @ com.jgutierrezgil.bmusic.playeractivity.initvariables(playeractivity.java:41) 06-03 22:04:47.023: e/androidruntime(1191):     @ com.jgutierrezgil.bmusic.playeractivity.oncreate(playeractivity.java:32) 06-03 22:04:47.023: e/androidruntime(1191):     @ android.app.activity.performcreate(activity.java:5231) 06-03 22:04:47.023: e/androidruntime(1191):     @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 06-03 22:04:47.023: e/androidruntime(1191):     @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 06-03 22:04:47.023: e/androidruntime(1191):     ... 11 more 06-03 22:04:49.193: i/process(1191): sending signal. pid: 1191 sig: 9 

manifest

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.jgutierrezgil.bmusic"     android:versioncode="1"     android:versionname="1.0" >      <uses-sdk         android:minsdkversion="15"         android:targetsdkversion="19" />      <application         android:allowbackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/apptheme" >         <activity             android:name="com.jgutierrezgil.bmusic.playeractivity"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>     </application>  </manifest> 

thank's in advance help.

you add fragment transaction, try find seekbar view before in view hierarchy. seekbar not found, , therefore null, nullpointerexception when calling setmax on it.

you should remove these 2 lines playeractivity.oncreate():

    mseekbar = (seekbar) findviewbyid(r.id.seekbar1);     mseekbar.setmax(mplayer.getduration()); 

and instead put them in placeholderfragment.oncreateview() after inflating rootview, know seekbar found, this:

    @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {         view rootview = inflater.inflate(r.layout.fragment_player,                 container, false);         mseekbar = (seekbar) rootview.findviewbyid(r.id.seekbar1);         mseekbar.setmax(mplayer.getduration());         return rootview;     } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -