Problem calling element with findViewById

0

Dear, I have a function called Reproductor() , the purpose of this is to use the class MediaPlayer , both to play and to give pause.

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import java.io.IOException;


public class Reproductor extends Activity {
public String urlMediaPlayer ="url-media";
public MediaPlayer mediaPlayer = new MediaPlayer();


ProgressBar progressBar;
public void onCreate(Bundle savedInstance) {
    System.out.println("Reproductor, onCreate");
    super.onCreate(savedInstance);

    setContentView(R.layout.activity_container);
    this.progressBar = (ProgressBar) findViewById(R.id.fabProgressBar);
    reproduceMedia();
}

    public Reproductor() {
}

public void reproduceMedia()
{
    cargaMedia cmedia = new cargaMedia();
    cmedia.execute();
}

public class cargaMedia extends AsyncTask<Void,Integer,Boolean> {

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }
    @Override
    protected void onCancelled() {
        super.onCancelled();
    }
    @Override
    protected void onPreExecute() {

       progressBar.setVisibility(View.VISIBLE);
    }
    @Override
    protected void onPostExecute(Boolean aBoolean) {
        progressBar.setVisibility(View.INVISIBLE);
    }
    @Override
    protected Boolean doInBackground(Void... params) {

        try{
           mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
           mediaPlayer.setDataSource(urlMediaPlayer);
           mediaPlayer.prepare();
           mediaPlayer.setVolume(1,1);
           mediaPlayer.start();
       }catch (IOException e)

       {
           e.printStackTrace();
       }

           return true;
    }

}

public void pausaMedia() throws IOException{

    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setDataSource(urlMediaPlayer);
    mediaPlayer.setVolume(0,0);
    mediaPlayer.pause();

}

} This function is called from class ContainerActivity , pressing a FloatingActionButton .

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabSelectListener;

import cl.cooperativa.readxmlfrominternetmaterial.R;
import cl.cooperativa.readxmlfrominternetmaterial.Reproductor;
import 
cl.cooperativa.readxmlfrominternetmaterial.view.fragment.DeportesFragment;
import 
cl.cooperativa.readxmlfrominternetmaterial.view.fragment.HomeFragment;
import 
cl.cooperativa.readxmlfrominternetmaterial.view.fragment.PoliticaFragment;

public class ContainerActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_container);

    BottomBar bottomBar = (BottomBar) findViewById(R.id.bottonbar);
    bottomBar.setDefaultTab(R.id.portada);


    HomeFragment homeFragment = new HomeFragment();


 getSupportFragmentManager().beginTransaction()
.replace(R.id.container,homeFragment)
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack(null).commit();

    FloatingActionButton floatingActionButton = (FloatingActionButton) 
     findViewById(R.id.fab);


    floatingActionButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

           // Reproductor reproductor = new Reproductor();

           // reproductor.reproduceMedia();

            Intent intent = new Intent(ContainerActivity.this, 
    Reproductor.class);
            startActivity(intent);

            /*Snackbar.make(view, "Replace with your own action", 
     Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();*/
        }

    });

    bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelected(@IdRes int tabId) {
    switch (tabId){
        case R.id.portada:
            HomeFragment homeFragment = new HomeFragment();

          getSupportFragmentManager().beginTransaction()
         .replace(R.id.container,homeFragment)

         .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
        .addToBackStack(null).commit();
            break;
       case R.id.deportes:
            DeportesFragment deportesFragment = new DeportesFragment();
            getSupportFragmentManager().beginTransaction()
           .replace(R.id.container,deportesFragment)

         .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
         .addToBackStack(null).commit();
            break;
        case R.id.politica:
            PoliticaFragment politicaFragment = new PoliticaFragment();
            getSupportFragmentManager().beginTransaction()
           .replace(R.id.container,politicaFragment)

          .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
           .addToBackStack(null).commit();
            break;
       }
    }
  });

}


@Override
protected void onPause()
{
    super.onPause();
}
protected void onResume() {

    super.onResume();
}
protected void onDestroy() {
    super.onDestroy();

  }
}

I would like to see the progressbar for as long as the audio is loaded, and once it is ready, it will disappear and I will show a pause button. But with the code I have when I click on floating actionbutton send me an error of NullPointerException .

  

PID: 936
  java.lang.NullPointerException: Attempt to invoke virtual method   'android.view.View android.view.Window.findViewById (int)' on a null   object reference                                                                                          at android.app.Activity.findViewById (Activity.java:2090)                                                                                          at cl.cooperativa.readxmlfrominternetmaterial.Reproductor.   (Player.java:21)                                                                                          at cl.cooperativa.readxmlfrominternetmaterial.view

     

.ContainerActivity $ 1.onClick (ContainerActivity.java:42)                                                                                          at android.view.View.performClick (View.java:5198)                                                                                          at android.view.View $ PerformClick.run (View.java:21147)                                                                                          at android.os.Handler.handleCallback (Handler.java:739)                                                                                          at android.os.Handler.dispatchMessage (Handler.java:95)                                                                                          at android.os.Looper.loop (Looper.java:148)                                                                                          at android.app.ActivityThread.main (ActivityThread.java:5417)                                                                                          at java.lang.reflect.Method.invoke (Native Method)                                                                                          at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller   .run (ZygoteInit.java:726)                                                                                          at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)

What am I doing wrong?

    
asked by Rodrigo 04.07.2017 в 20:02
source

1 answer

0

Here is the problem:

public class Reproductor extends Activity {
  //..
  ProgressBar progressBar = (ProgressBar) findViewById(R.id.fabProgressBar);

The findViewById method works together setContentView .

  

setContentView assigns the content of the view from an XML recursod.

findViewById requires a view assigned to the Activity to work and to assign a view to Activity setContentView is used. And the propitious place to assign the view with setContentView to an Activity is overwriting the method onCreate of Activity

I do not see that you assign the view to Activity Reproductor . You must assign a view to the Activity and after having assigned the view you are looking for the element:

public class Player extends Activity {

 ProgressBar progressBar;

 public void OnCreate(Bundle saveInstance)
 { 
    setContentView(R.layout.vistaConElLoadingRing);
    this.progressBar = (ProgressBar) findViewById(R.id.fabProgressBar);
 }
//...

Note: I recommend you read about the Activity and its life cycles in the documentation official so that you have a more solid idea of what they do. Get a soda and popcorn because it will be a long read.

    
answered by 04.07.2017 в 20:56