Espero me puedan ayudar :) he estado atorado en esto ya algun tiempo. Estoy ejecutando una app que llena datos a Mysql y al probar el método en una Actividad me funciona normalmente, pero al ejecutarlo dentro de un fragment me da un error cuando lo ejecuto.
Este es el código de mi 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());
}
});
}
}
y este es el error que me muestra
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)
muchas gracias por su ayuda