I'm creating a fragment where I have in one part the map and in another a Retrofit object, where I get the date and time of a server. The error occurs inside the onResponse call of Retrofit, where I try to set the TextView with the date and time obtained from my server. The same thing happens when I load an image from the server and try to play it inside an ImageView created in the same fragment.
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 for the help!