片段中片段之间的通信

时间:2016-05-09 02:05:32

标签: java android listview android-fragments

我有两个片段(ProductDisplayFragment和PaymentFragment),它们在NavigationActivity的片段中相互通信。 ProductDisplayFragment显示用户点击以将产品添加到PaymentFragment的ListView中的产品列表。

每次用户在ProductDisplayFragment下的GridView中点击产品时,它都会自动更新为PaymentFragment ListView。我一直在努力弄清楚我的代码有什么问题,但我仍然无法找到它。

当用户在PaymentFragment中添加产品时,PaymentFragment将首先与NavigationActivity进行通信,NavigationActivity将与ProductDisplayFragment通信以更新其ListView中的产品。

错误如下:

java.lang.NullPointerException: Attempt to invoke virtual method 'void test.payment.PaymentFragment.getProduct(test.productdisplay.Product)' on a null object reference 

这是我的代码:

NavigationActivity.java

public class NavigationActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener,
        ProductDisplayFragment.SendProduct {

    private ProductDisplayFragment.SendProduct sendProduct;
    private static ArrayList<Product> PRODUCTS_DATABASE = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Firebase.setAndroidContext(this);
        setContentView(R.layout.activity_navigation);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setElevation(0);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        // Display default fragment as MainFragment when navigation activity is launched
        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
        tx.replace(R.id.container, new MainFragment());
        tx.commit();

        createProductDatabase();
        getProductsData();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    // Code eliminated

    @Override
    public void sendProduct(Product product) {
        PaymentFragment paymentFragment = (PaymentFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_payment_list);
        Log.d(getLocalClassName(), product.getName());
        paymentFragment.getProduct(product);
    }
}

ProductDisplayFragment.java

public class ProductDisplayFragment extends Fragment {

    private static final String MESSAGE_SUCCESS_ADDED_PRODUCT = "%1$s added successfully";
    private static final String MESSAGE_ERROR_IMPLEMENTATION = "Implementation of sendProduct method required";

    private GridView gridView;
    private ProductGridAdapter productAdapter;
    private SendProduct sendProduct;

    public ProductDisplayFragment() {
        // Required empty public constructor
    }

    public interface SendProduct {
        void sendProduct(Product product);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_product_display, container, false);

        gridView = (GridView) rootView.findViewById(R.id.gridview);
        productAdapter = new ProductGridAdapter(getActivity(), R.layout.fragment_product_display, getProductList());
        gridView.setAdapter(productAdapter);

        return rootView;
    }

    // Code eliminated

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            sendProduct = (SendProduct) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(MESSAGE_ERROR_IMPLEMENTATION);
        }
    }
}

PaymentFragment.java

public class PaymentFragment extends Fragment {

    private static final String MESSAGE_VOID_DIALOG_TITLE = "Void Payment";
    private static final String MESSAGE_VOID_DIALOG_MESSAGE = "Are you sure you want to clear all fields and start from scratch?";
    private static final String MESSAGE_NOTES_DIALOG_TITLE = "Enter Notes";
    private static final String MESSAGE_VOID_SUCCESSFULLY_VOIDED = "All products cleared successfully";
    private static final String MESSAGE_NOTES_SUCCESSFULLY_SAVED = "Notes saved successfully";
    private static final String MESSAGE_SUCCESSFULLY_SAVED = "Current order is saved successfully";
    private static final String MESSAGE_OK = "Ok";
    private static final String MESSAGE_CANCEL = "Cancel";

    private ListView listView;
    private PaymentListAdapter paymentAdapter;
    private List<Product> productList;
    private String notes;

    private LinearLayout voidButton;
    private LinearLayout saveButton;
    private LinearLayout noteButton;
    private LinearLayout discountButton;
    private LinearLayout payButton;
    private TextView emptyList;


    public PaymentFragment() {
        productList = new ArrayList<>();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_payment, container, false);

        // TODO: Remove hardcoded products once add functions are working
        productList = getProductList();

        listView = (ListView) rootView.findViewById(android.R.id.list);
        paymentAdapter = new PaymentListAdapter(getActivity(), R.layout.fragment_payment, productList);
        listView.setAdapter(paymentAdapter);

        voidButton = (LinearLayout) rootView.findViewById(R.id.button_void);
        saveButton = (LinearLayout) rootView.findViewById(R.id.button_save);
        noteButton = (LinearLayout) rootView.findViewById(R.id.button_note);
        discountButton = (LinearLayout) rootView.findViewById(R.id.button_discount);
        payButton = (LinearLayout) rootView.findViewById(R.id.button_pay);
        emptyList = (TextView) rootView.findViewById(R.id.empty_list);

        setTextView();
        buttonsOnClick();

        return rootView;
    }

    // Code eliminated
    public void getProduct(Product product) {
        productList.add(product);
        paymentAdapter = new PaymentListAdapter(getActivity(), R.layout.fragment_payment, productList);
        listView.setAdapter(paymentAdapter);
    }
}

0 个答案:

没有答案