NullPointer error when ViewPager instantiates on Android

1

When entering an Activity and instantiating ViewPager it returns null and fails to instantiate the object. In another Activity I have exactly the same thing and if it does, what could I have wrong? I attach the code. The error starts from here:

this.pager = (ViewPager) this.findViewById(R.id.viewPager);

But it's triggered here:

this.pager.setAdapter(adapter);

Class pfeActivity :

public class pfeActivity extends FragmentActivity {

/**
 * The pager widget, which handles animation and allows swiping horizontally
 * to access previous and next pages.
 */
ViewPager pager;

/**
 * The pager adapter, which provides the pages to the view pager widget.
 */
MyFragmentPagerAdapter pagerAdapter;

//Declaras una variable tipo Thread
private Thread HiloConsumo;

OperacionesBaseDatos bdOperaciones = new OperacionesBaseDatos();

TextView textoSaldo, textoPrice, textoDayHigh, textoDayLow, textoYearHigh, textoYearLow, textoOpenValue, textoChange;

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

    textoSaldo = (TextView) findViewById(R.id.saldoTextView);
    actualizarInfoSaldo();

    textoPrice = (TextView) findViewById(R.id.activityStockValue_textView);
    textoDayHigh = (TextView) findViewById(R.id.activityStockValorTop_textView);
    textoDayLow = (TextView) findViewById(R.id.activityStockValorLow_textView);
    textoYearHigh = (TextView) findViewById(R.id.activityStockValorAnoTop_textView);
    textoYearLow = (TextView) findViewById(R.id.activityStockValorAnoLow_textView);
    textoOpenValue = (TextView) findViewById(R.id.activityStockApertura_textView);
    textoChange = (TextView) findViewById(R.id.activityStockPercent_textView);

    ImageView graph = (ImageView) findViewById(R.id.graphImageView);
    //graph.setImageResource(R.drawable.graph);

    //Picasso.with(this).load("http://chart.finance.yahoo.com/z?s=INTC").into(graph);
    Picasso.with(this).load("https://www.google.com/finance/getchart?q=PFE&p=20Y&i=86400").into(graph);

    // Instantiate a ViewPager
    this.pager = (ViewPager) this.findViewById(R.id.viewPager);

    // Create an adapter with the fragments we show on the ViewPager
    MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(
            getSupportFragmentManager());
    adapter.addFragment(ScreenSlidePageFragment.newInstance(getResources()
            .getColor(R.color.background2), 0));
    adapter.addFragment(ScreenSlidePageFragment.newInstance(getResources()
            .getColor(R.color.background2), 1));
    this.pager.setAdapter(adapter);

    // Se crea el hilo
    HiloConsumo = new HiloConsumidor("PFE");
    HiloConsumo.setName("HiloConsumidor");
    HiloConsumo.start();

}
@Override
public void onStop() {
    // Se desregistra del bus por defecto.
    EventBus.getDefault().unregister(this);
    super.onStop();
}
//En caso de reinicio de la actividad
@Override
protected void onRestart() {
    super.onRestart();
    actualizarInfoSaldo();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {
    textoPrice.setText(String.valueOf(event.price));
    textoOpenValue.setText("Valor Apertura: " + String.valueOf(event.openValue));
    textoDayHigh.setText("Máximo: " + String.valueOf(event.dayHigh));
    textoDayLow.setText("Mínimo: " + String.valueOf(event.dayLow));
    textoYearHigh.setText("Máximo Anual: " + String.valueOf(event.yearHigh));
    textoYearLow.setText("Minimo Anual:  " + String.valueOf(event.yearLow));
    textoChange.setText("Porcentaje: " + String.valueOf(event.change) + "%");
    System.out.println("FINAL");
}
// Metodo que obtiene el saldo que dispone la cuenta y lo muestra en pantalla
private void actualizarInfoSaldo() {
    Saldo saldo;
    saldo = bdOperaciones.leerSaldo();
    String saldoStr = "Saldo actual: " + String.valueOf(saldo.getSaldo()) +" $";
    CharSequence charSqc;
    charSqc=saldoStr;
    textoSaldo.setText(charSqc);
}
// Metodo que al pulsar el boton de retroceder vuelve al Main y mata el HiloConsumidor
public void onBackPressed() {
    if (HiloConsumo != null)//valida si existe instancia de Thread.
    {
        HiloConsumo.interrupt();  //Interrumpe su ejecución.
        super.onBackPressed();
    }
}
}

This is the error:

FATAL EXCEPTION: main
  Process: com.example.iberd.actionvalue, PID: 1672
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.iberd.actionvalue/com.example.iberd.actionvalue.actionValues.pfeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
      at android.app.ActivityThread.-wrap12(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6119)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
      at com.example.iberd.actionvalue.actionValues.pfeActivity.onCreate(pfeActivity.java:78)
      at android.app.Activity.performCreate(Activity.java:6679)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:154) 
      at android.app.ActivityThread.main(ActivityThread.java:6119) 
      at java.lang.reflect.Method.invoke(Native Method)
    
asked by Eduardo 23.05.2017 в 23:19
source

1 answer

2

The error indicates that you are trying to call setAdapter in a null reference.

  

java.lang.NullPointerException: Attempt to invoke virtual method 'void   android.support.v4.view.ViewPager.setAdapter (android.support.v4.view.PagerAdapter) '   on a null object reference

The ViewPager with id viewPager is not found in Layout activity_pfe.xml , for this reason you are getting pager with null value.

You do not need references with this , simply:

pager = (ViewPager) this.findViewById(R.id.viewPager);
//this.pager = (ViewPager) this.findViewById(R.id.viewPager);
    
answered by 24.05.2017 / 00:13
source