Cast problem in onAttach method

Asked

Viewed 30 times

1

I am making an application that creates graphs, I am creating the implementing of the nodes via Dialog where the usurious selects us and creates a arrest but this giving Cast error.

public class DialogAddAresta extends AppCompatDialogFragment {

    private String selectedNodeTo, selectedNodeFrom;
    private Spinner spTo, spFrom;
    private List<String> listNodes;
    private int posTo, posFrom;
    private ExampleDialogListner listner;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(Objects.requireNonNull(getActivity()));

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.layout_dialog_add_aresta, null);

        listNodes = getArguments().getStringArrayList("list");

        spTo = view.findViewById(R.id.sp_to);
        spFrom = view.findViewById(R.id.sp_from);

        ArrayAdapter<String> nodes = new ArrayAdapter<>(getActivity(),android.R.layout.simple_spinner_item, listNodes);
        nodes.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spTo.setAdapter(nodes);
        spFrom.setAdapter(nodes);

        builder.setView(view)
                .setTitle("Add Aresta")
                .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        posTo = spTo.getSelectedItemPosition();
                        posFrom = spFrom.getSelectedItemPosition();
                        selectedNodeTo = listNodes.get(posTo);
                        selectedNodeFrom = listNodes.get(posFrom);
                        listner.applyNodes(selectedNodeTo, selectedNodeFrom);
                        Toast.makeText(getActivity(),"node to "+selectedNodeTo+ " from "+selectedNodeFrom,Toast.LENGTH_SHORT).show();
                    }
                });

        return builder.create();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            listner = (ExampleDialogListner) context;
        } catch (ClassCastException e){
            throw new ClassCastException(context.toString() + "must implement ExampleDialogListner");
        }
    }

    public interface ExampleDialogListner{
        void applyNodes(String nodeTo, String nodeFrom);
    }
}

my class tab that creates the Fragments:

public class Tab extends AppCompatActivity implements DialogAddAresta.ExampleDialogListner {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);

        TabLayout tabLayout = findViewById(R.id.tablayout_id);
        ViewPager viewPager = findViewById(R.id.viewpager_id);

        ViewPageAdapter adapter = new ViewPageAdapter(getSupportFragmentManager());
        adapter.AddFragment(new FragmentGrafo(), "Grafo");
        adapter.AddFragment(new FragmentAlgoritmo(), "Algoritmo");

        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    }

}

My Fragment Class is here that I want what the user has selected:

public class FragmentGrafo extends Fragment  implements DialogAddAresta.ExampleDialogListner {

    private static final int CONTENT_VIEW_ID = 10101010;
    private DefaultFragment fragment;
    private Graph graph;
    private String[] criaNos = new String[]{"A","B","C","D","E","F","G","H","I","J","K",
            "L","M","N","O","P","Q","R","S","T","U","V","X","W","Y","Z"};
    private int pos = 0;
    private float x = 0.0f;
    private float y = 0.0f;
    private String styleSheet = "graph { fill-color: #EEE; }" +
            "node { size: 100; text-size: 30; fill-color: white; stroke-mode: plain;}";
    public List<String> nos = new ArrayList<>();
    private ArrayAdapter<String> adapter;
    private List<String> criaArestas = new ArrayList<>();
    private int arestaPos = 0;
    private String nodeTo, nodeFrom;
    TextView txtTips;

    public FragmentGrafo(){

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       final View view = inflater.inflate(R.layout.grafo_fragment, container, false);
        txtTips = view.findViewById(R.id.txtView);
       BottomNavigationView bottomNavigationView = view.findViewById(R.id.bottomNavView);
       bottomNavigationView.setOnNavigationItemSelectedListener(selectedListener);
       return view;
    }

    private BottomNavigationView.OnNavigationItemSelectedListener selectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_add_node:
                    if (pos < criaNos.length) {
                        graph.addNode(criaNos[pos]);
                        graph.getNode(criaNos[pos]).setAttribute("ui.label", criaNos[pos]);
                        graph.getNode(criaNos[pos]).setAttribute("xy", x, y);
                        nos.add(criaNos[pos]);
                        pos++;
                        x += 1.0f;
                        y += 1.5f;
                    }else {
                        Toast.makeText(getActivity(), "Não pode adcionr mais nós", Toast.LENGTH_SHORT).show();
                    }

                    return true;

                case R.id.action_add_aresta:
                    openDialogAddAresta();

                    return true;

                case R.id.action_remover:
                    txtTips.setText("Azul");
                    Toast.makeText(getActivity(), "Ultima aresta "+criaArestas.toString(), Toast.LENGTH_SHORT).show();
                    return true;
            }
            return false;
        }
    };

    @Override
    public void onCreate(@Nullable android.os.Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        graph = new MultiGraph("AthenaGraph");

        graph.setAttribute( "ui.stylesheet", styleSheet);
        graph.setAttribute("ui.antialias");

        display(savedInstanceState, graph, false);
    }

    public void display(Bundle savedInstanceState, Graph graph, boolean autoLayout) {
        if (savedInstanceState == null) {
            FragmentManager fm = getActivity().getFragmentManager();
            fragment = (DefaultFragment) fm.findFragmentByTag("fragment_tag");

            if (null == fragment) {
                fragment = new DefaultFragment();
                fragment.init(graph, autoLayout);
            }

            FragmentTransaction ft = fm.beginTransaction() ;
            ft.add(R.id.layoutFragment, fragment).commit();
        }
    }

    public void openDialogAddAresta() {
        DialogAddAresta dialogAddAresta = new DialogAddAresta();

        Bundle bundle = new Bundle();
        bundle.putStringArrayList("list", (ArrayList<String>)nos);

        dialogAddAresta.setArguments(bundle);
        dialogAddAresta.show(getActivity().getSupportFragmentManager(), "Add Aresta");
    }

    @Override
    public void applyNodes(String selectedNodeTo, String selectedNodeFrom) {
        Toast.makeText(getActivity(), "FragmentGrafo "+selectedNodeTo+" "+selectedNodeFrom, Toast.LENGTH_SHORT).show();
    }
}
  • 1

    And where is the code where you instantiate the Fragment?

  • ola, then I realized that it is returning the value that the user selects for TAB as do for the value back to fragmentGrafo?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.