Nullpointerexception when using setText for a Textview in Fragment

Asked

Viewed 282 times

1

I am creating a Fragment where I have in one part the map and in another an object of Retrofit, where I get date and time from a server. The error occurs inside the Retrofit onResponse call, where I try to set Textview with the date and time obtained from my server. The same happens when I upload an image from the server and try to play it inside an Imageview created in the Facebook itself.

This is my code:

@BindView(R.id.oi) TextView dataHora;

@Override
public void onCreate(Bundle bundle) {
    LayoutInflater inflater = (LayoutInflater)getLayoutInflater();
    View v = inflater.inflate(R.layout.activity_mapa, null);
    super.onCreate(bundle);
    getMapAsync(this);
    ButterKnife.bind((Activity) getContext());
}
    @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    centralizaImagem(googleMap);

    circuloNoMapa();

    Call<List<AppSync>> calldata = (Call<List<AppSync>>) new RetrofitInicializador().getServices().lista();
    calldata.enqueue(new Callback<List<AppSync>>() {
        @Override
        public void onResponse(Call<List<AppSync>> call, Response<List<AppSync>> response) {
            AppSync objeto = response.body().get(0);

            String url = objeto.getUrl();
            String fileDate = objeto.getFileDate();
            String fileTime = objeto.getFileTime();

            dataHora.setText(fileDate + " - " + fileTime + " GTM") ;

            convertido = new ImageView(getContext());

            Picasso.with(getContext())
                    .load(url)
                    .into(convertido);

           convertido.setDrawingCacheEnabled(true);
           convertido.buildDrawingCache();
           Bitmap bitmap= Bitmap.createBitmap(convertido.getDrawingCache());

            configuraBmpMapa();

        }

        @Override
        public void onFailure(Call<List<AppSync>> call, Throwable t) {
            Log.e("ERRO RETROFIT", t.getMessage() );
        }
    });

}

private void configuraBmpMapa() {
    LatLngBounds newarkBounds = new LatLngBounds(
            new LatLng(LATITUDE_MINIMA, LONGITUDE_MINIMA),       // South west corner
            new LatLng(LATITUDE_MAXIMA, LONGITUDE_MAXIMA));      // North east corner

    GroundOverlayOptions newarkMap = new GroundOverlayOptions()
            .image(BitmapDescriptorFactory.fromResource(R.drawable.oi))
            .positionFromBounds(newarkBounds);

    GroundOverlay imageOverlay = mMap.addGroundOverlay(newarkMap);
}

This is the error message:

FATAL EXCEPTION: main
Process: com.tabatabaesso.exercicio2, PID: 9585
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
     at com.tabatabaesso.exercicio2.MapaFragment$1.onResponse(MapaFragment.java:102)
     at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
     at android.os.Handler.handleCallback(Handler.java:789)
     at android.os.Handler.dispatchMessage(Handler.java:98)
     at android.os.Looper.loop(Looper.java:164)
     at android.app.ActivityThread.main(ActivityThread.java:6541)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

This is the . xml for the text:

<FrameLayout
    android:id="@+id/frame_datahora"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1">
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/oi"
    tools:text="02/09/99 - 11:00 GTM"
    android:textAlignment="center"
    android:textSize="16sp"/>
</FrameLayout>

Thanks in advance for the help!

1 answer

1


The views bind in Fragments is different from activities, according to the documentation. You should invoke:

@Override
public void onCreate(Bundle bundle) {
    LayoutInflater inflater = (LayoutInflater)getLayoutInflater();
    View v = inflater.inflate(R.layout.activity_mapa, null);
    super.onCreate(bundle);
    getMapAsync(this);
    ButterKnife.bind(this, v);
}

I recommend to read also about the Binding Reset.

  • By the way, thank you so much for your help! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.