Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Amal Abdull
Amal Abdull
1,629 Points

Fragment cast error

Hi , in Fragment Course I get some error in displaying the list and the error I get in this line

OnRecipeSelectedInterface listener = (OnRecipeSelectedInterface) getActivity();

in ListFragment

I get this error

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example., PID: 30765 java.lang.ClassCastException: com.example.altqniah.MainActivity cannot be cast to com.example.altqniah.ListBooks$OnRecipeSelectedInterface at com.example.altqniah.ListBooks.onCreateView(ListBooks.java:22)

and no matter i try solution , nothing works

Ross Logan
Ross Logan
8,131 Points

Hi,

Can you paste the rest of your code and I can try to help you out with what it is that you are trying to do?

Amal Abdull
Amal Abdull
1,629 Points

MainActivity

package com.example.kiennguyen.smellslikebakin;

import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements ListFragment.OnRecipeSelectedInterface {

public static final String LIST_FRAGMENT = "list_fragment";
public static final String VIEWPAGER_FRAGMENT = "viewpager_fragment";

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

    ArrayList<String> al ,al2 ;
    al = new ArrayList<String>();
    al2 = new ArrayList<String>();
    System.out.println("Initial size of al: " + al.size());

    // add elements to the array list
    al.add("C");
    al.add("A");
    al.add("E");
    al.add("B");
    al.add("C");
    al.add("A");
    al.add("E");
    al.add("B");

    al2.add("C");
    al2.add("A");
    al2.add("E");
    al2.add("B");
    al2.add("C");
    al2.add("A");
    al2.add("E");
    al2.add("B");
    Recipes.setList(al); // set list
    Recipes.setList2(al2); // set list

    BlankFragment savedFragment = (BlankFragment) getSupportFragmentManager()
            .findFragmentByTag(LIST_FRAGMENT);
    if (savedFragment == null) {
        BlankFragment fragment = new BlankFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.placeHolder, fragment, LIST_FRAGMENT);
        fragmentTransaction.commit();
    }
}

@Override
public void onListRecipeSelected(int position) {

}

}

BlankFragment

import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;

/**

  • Created by amal on 11/16/16. */

public class BlankFragment extends Fragment implements View.OnClickListener ,ListFragment.OnRecipeSelectedInterface {

public static final String LIST_FRAGMENT = "list_fragment";
private View mView;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.fragment_blank, container, false);
    start();
    return mView;
}

private void start() {

    FragmentManager fragmentManager =getFragmentManager();

    ListFragment savedFragment = (ListFragment)
            fragmentManager.findFragmentByTag(LIST_FRAGMENT);
    if (savedFragment == null) {
        ListFragment fragment = new ListFragment();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_blank, fragment, LIST_FRAGMENT);
        fragmentTransaction.commit();
    }
}


@Override
public void onListRecipeSelected(int position) {

}

}

ListFragment

import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;

public class ListFragment extends Fragment {

public interface OnRecipeSelectedInterface {
    void onListRecipeSelected(int position);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    OnRecipeSelectedInterface listener = (OnRecipeSelectedInterface) getActivity();
    View view = inflater.inflate(R.layout.fragment_list, container, false);

    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.listRecyclerView);
    ListAdapter listAdapter = new ListAdapter(listener);
    recyclerView.setAdapter(listAdapter);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);

    return view;
}

}

ListAdapter

import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;

public class ListAdapter extends RecyclerView.Adapter { private final ListFragment.OnRecipeSelectedInterface mListener;

public ListAdapter(ListFragment.OnRecipeSelectedInterface listener) {
    this.mListener = listener;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater
            .from(parent.getContext())
            .inflate(R.layout.list_item, parent, false);
    return new ListViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    ((ListViewHolder) holder).bindView(position);
}

@Override
public int getItemCount() {
    return Recipes.list.size();
}

private class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private TextView mTextView2;
    private TextView mTextView;
    private int mPosition;

    public ListViewHolder(View itemView) {
        super(itemView);
        mTextView2 = (TextView) itemView.findViewById(R.id.itemText2);
        mTextView = (TextView) itemView.findViewById(R.id.itemText);
        itemView.setOnClickListener(this);
    }

    public void bindView(int position) {
        mTextView2.setText(Recipes.list2.get(position));;
        mTextView.setText(Recipes.list.get(position));
        mPosition = position;
    }

    @Override
    public void onClick(View v) {
        mListener.onListRecipeSelected(mPosition);
    }
}

}

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

Make sure that your MainActivity class implements the interface. The cast error is happening because it currently doesn't.

Amal Abdull
Amal Abdull
1,629 Points

ok instead from run it from activity i run it from fragment and implement the interface

Seth Kroger
Seth Kroger
56,413 Points

From the error message you posted indicates the problem is in a class called ListBooks that also has an inner interface of the same name, OnRecipeSelectedInterface. ListBooks.OnRecipeSelectedInterface and ListFragment.OnRecipeSelectedInterface are two separate interfaces and MainActivity only implements the latter. You need to implement the former as well if you want it to work with both. I also recommend changing the name of the interface and it's method to avoid confusion and naming conflicts.

Amal Abdull
Amal Abdull
1,629 Points

I edit it but still the same problem What is want to do is from main activity start Blank Fragment then from it start List Fragment