0

So I have to convert an activity into a fragment. I followed some basic principles however the app still crashes.

The converted activity code:

package com.example.computerscienceiawithtablayout;

import android.support.annotation.NonNull;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import java.io.File;
import java.util.*;

public class FragmentHome extends Fragment implements AddBookDialogue.AddBookDialogueListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.fragment_home, container, false);
}
    @Override
    public void onStart() {
        super.onStart();
    }
    public void onBigButtonClick(View w) {
        AddBookDialogue addBookDialogue = new AddBookDialogue();
        addBookDialogue.show(getChildFragmentManager(), "add book dialogue");
    }

    @Override
    public void getTexts(String bookAuthor, String bookTitle, String bookBarcode, String bookCourse, String selectedBookshelf) {
        if(bookAuthor.isEmpty() || bookTitle.isEmpty() || bookBarcode.isEmpty() || bookCourse.isEmpty()){
          Toast.makeText(getActivity(), "You didn't enter all necessary data", Toast.LENGTH_LONG).show();
        }else{
            if(bookBarcode.length()!=8){
                Toast.makeText(getActivity(), "Barcodes should be eight digits long", Toast.LENGTH_LONG).show();
            }else{ if(selectedBookshelf.equals("Withdrawn")){
                Book a = new Book(bookAuthor, bookTitle, bookBarcode, bookCourse, selectedBookshelf);
                Toast.makeText(getActivity(), a.toString(), Toast.LENGTH_LONG).show();

            }else{
                Book a = new Book(bookAuthor, bookTitle, bookBarcode, bookCourse, selectedBookshelf);
                Toast.makeText(getActivity(), a.toString(), Toast.LENGTH_LONG).show();

            }

            }
        }

    }

    }

The fragment call upon an addBookDialogue, which is a dialoguefragment, code here:

package com.example.computerscienceiawithtablayout;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class AddBookDialogue extends AppCompatDialogFragment{
    private EditText editTextBookAuthor;
    private EditText editTextBookTitle;
    private EditText editTextBookCourse;
    private EditText editTextBookBarcode;
    private Spinner spinnerBookshelf;
    private AddBookDialogueListener listener;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getParentFragment().getActivity());
        LayoutInflater inflater = getParentFragment().getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialogue_addbook, null);
        builder.setView(view)
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String bookAuthor =  editTextBookAuthor.getText().toString();
                        String bookTitle =  editTextBookTitle.getText().toString();
                        String bookBarcode =  editTextBookBarcode.getText().toString();
                        String bookCourse =  editTextBookCourse.getText().toString();
                        String seletecBookshelf = spinnerBookshelf.getSelectedItem().toString();

                        listener.getTexts( bookAuthor, bookTitle, bookBarcode, bookCourse, seletecBookshelf);

                    }
                });
        editTextBookAuthor = view.findViewById(R.id.author);
        editTextBookTitle= view.findViewById(R.id.bookTitle);
        editTextBookBarcode= view.findViewById(R.id.bookBarcode);
        editTextBookCourse= view.findViewById(R.id.bookBarcode);
        spinnerBookshelf= view.findViewById(R.id.spinnerBookshelf);
        ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(getActivity(), R.array.addBookBookshelfsAvailable, android.R.layout.simple_spinner_item);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerBookshelf.setAdapter(spinnerAdapter);
        return builder.create();
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            listener = (AddBookDialogueListener) context;
        } catch(ClassCastException e){
            throw new ClassCastException(context.toString() + "no listener");
        }
    }
    public interface AddBookDialogueListener{
        void getTexts(String bookAuthor, String bookTitle, String bookBarcode, String bookCourse, String selectedBookshelf);

    }

}

I am huge newbie and basing some of this code on intuition. The major flaws that I can think of are calling the addBookDialogue with .show(getChildFragmentManager) and in AddBookDialogue file setting the builder to getParentFragment.getActivity This probably isn't the correct approach, but I am having difficulties understanding the interactions between fragments. The book object is a simple java class with a constructor and a few setters/getters

So yeah, idk where to problem is, all help appreciated :)

For more reference here is the xml file for the main activity

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".FragmentHome">
    <fragment
            android:id="@+id/fragmentHome"
            android:name="com.example.computerscienceiawithtablayout.FragmentHome"
            android:layout_height="match_parent"
            android:layout_width="match_parent"/>


</android.support.constraint.ConstraintLayout>

and here is the xml for the fragment

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".FragmentHome">

    <Button
            android:text="@string/Add"
            android:textStyle="bold"
            android:textColor="#ffffff"
            android:background="@drawable/button_states"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:onClick="onBigButtonClick"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="480dp" app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="164dp" style="@style/Widget.AppCompat.Button"/>


</android.support.constraint.ConstraintLayout>

0 Answers0