I hope you can help me :) I've been stuck in this and some time. I am running an app that fills Mysql with data and when I try the method in an Activity it works normally, but when I execute it in a fragment it gives me an error when I execute it.
This is the code of my fragment.
public class Selection3 extends Fragment {
ImageView icon_card, icon_efectivo;
TextView efectivo, card;
Button botonlisto;
LinearLayout seleccioncard;
CompositeDisposable compositeDisposable;
KAPI mService;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_selection3, container, false);
icon_card=(ImageView) v.findViewById(R.id.icon_card);
seleccioncard=(LinearLayout) v.findViewById(R.id.seleccion_card);
seleccioncard.setVisibility(View.INVISIBLE);
icon_efectivo=(ImageView) v.findViewById(R.id.icon_efectivo);
efectivo=(TextView) v.findViewById(R.id.txt_efectivo);
card=(TextView) v.findViewById(R.id.txt_card);
botonlisto=(Button) v.findViewById(R.id.botonlisto);
botonlisto.setClickable(true);
//BOTON RESUMIR COMPRA
botonlisto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
placeOrder();
initDB();
}
});
return v;
}
private void initDB() {
Common.edmtRoomDatabase = EDMTRoomDatabase.getInstance(getActivity());
Common.cartRepository = CartRepository.getInstance(CartDataSource.getInstance(Common.edmtRoomDatabase.cartDAO()));
Common.favoriteRepository = FavoriteRepository.getInstance(FavoriteDataSource.getInstance(Common.edmtRoomDatabase.favoriteDAO()));
}
private void placeOrder() {
//DIALOGO PARA CONFIRMAR COMPRA Y SUBIR A MYSQL
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Confirmar Orden");
View submit_order_layout = LayoutInflater.from(getActivity()).inflate(R.layout.submit_order,null);
final EditText edt_comment = (EditText)submit_order_layout.findViewById(R.id.edt_comment);
builder.setView(submit_order_layout);
builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final String orderComment = edt_comment.getText().toString();
final String orderPhone =edt_comment.getText().toString();//telefono no asignado /prueba con comments;
//Submit order AQUI ME MUESTRA EL ERROR (linea134)
compositeDisposable.add(
Common.cartRepository.getCartItems()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Consumer<List<Cart>>() {
@Override
public void accept(List<Cart> carts) throws Exception {
if (!TextUtils.isEmpty(orderPhone))
sendOrderToServer(Common.cartRepository.sumPrice(),
carts,
orderComment,orderPhone);
else
Toast.makeText(getActivity(),"Ingrese un contacto",Toast.LENGTH_SHORT).show(); }
})
);
}
});
builder.show();
}
private void sendOrderToServer(float sumPrice, List<Cart> carts, String orderComment, String orderPhone) {
if (carts.size()>0)
{
String orderDetail = new Gson().toJson(carts);
//llenado detalles de la compra con phone por prueba
mService.submitOrder(String.valueOf(sumPrice),orderDetail,orderComment,orderPhone,orderPhone)
.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
Toast.makeText(getActivity(),"Orden Ingresada",Toast.LENGTH_SHORT).show();
//clear Cart
Common.cartRepository.emptyCart();
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e("ERROR",t.getMessage());
}
});
}
}
and this is the error that shows me
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean io.reactivex.disposables.CompositeDisposable.add (io.reactivex.disposables.Disposable)' on a null object reference at com.xxxxx.fragment.Selection3 $ 4.onClick (Selection3.java:134)
Thank you very much for your help