Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity cannot be cast to Interface (ClassCastException)

Tags:

java

android

I'm currently getting a ClassCastException in my android app caused by "activity cannot be cast to interface". Here is my code:

Logcat says the Exception is thrown in the onAttach part in MovieGridFragment on the line "this.clickListener = (clickInterfaceHelper) context;".

My Interface:

public interface clickInterfaceHelper {
void clickOnItem(int id);
void favoriteMovieItem(int movieId); }

The Fragment Class:

public class MovieGridFragment extends Fragment {

public clickInterfaceHelper clickListener;
private int index;
private GridView movieGridView;
public List<movieData> movieDataList = new ArrayList<>();

public MovieGridFragment() {} //empty constructor

@Override
public void onAttach(Context context) {
    this.clickListener = (clickInterfaceHelper) context;
    super.onAttach(context);
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    if(savedInstanceState != null) {
        if (!movieDataList.isEmpty()) {
            movieDataList = Arrays.asList((movieData[]) savedInstanceState.getSerializable("OLDMOVIEDATA"));
        }
    }
    super.onCreate(savedInstanceState);
}

The MainActivity:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    setHasOptionsMenu(true);


    View rootView = inflater.inflate(R.layout.movie_display_fragment, container, false);

    movieGridView = (GridView) rootView.findViewById(R.id.gv_movie_display);
    movieAdapter adapter = new movieAdapter(getActivity(),movieDataList);
    adapter.notifyDataSetChanged();
    movieGridView.setAdapter(adapter);
    movieGridView.setSelection(index);
    movieGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(clickListener != null)
                    clickListener.clickOnItem(position);

        }
    });
    return rootView;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    index = movieGridView.getFirstVisiblePosition();
    outState.putSerializable("OLDMOVIEDATA",movieData.movieDataArray);
    super.onSaveInstanceState(outState);
}}
and mainactivity:

public class MainActivity extends AppCompatActivity implements clickInterfaceHelper {

public static String sorterString = null;
public static String urlBase = "https://api.themoviedb.org/3/movie/";
public static String urlFinal = null;

RequestQueue requestQueue;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                .add(R.id.activity_container, new MovieGridFragment())
                .commit();
        movieData.movieDataPosition = 0;
    }
    if(savedInstanceState != null) {
        sorterString = savedInstanceState.getString("SORTER");
    }

    if(savedInstanceState == null)
        movieData.movieDataPosition = 0;

    if(sorterString==null)
        sorterString="popular?";
    if(sorterString!="favorite" && sorterString!=null) {
        if(networkChecker.isNetworkAvailableChecker(this)) {
            movieRequest();
        }
    }



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu_act, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(id == R.id.m_popularity_action) {
        if(sorterString != "popular?") {
            sorterString = "popular?";
            if(networkChecker.isNetworkAvailableChecker(this))
            movieRequest();
        }
        return true;
    }

    if(id == R.id.m_action_voter) {
        if(sorterString != "top_rated?") {
            sorterString = "top_rated?";
            if(networkChecker.isNetworkAvailableChecker(this))
            movieRequest();
        }
        return true;
    }

    if(id == R.id.m_favorite_btn) {
        if(sorterString != "favorite") {
            SQLiteOpenHelper helper = new movieDataDbHelper(this);
            SQLiteDatabase database = helper.getReadableDatabase();
            Cursor cursor= database.query(movieDataContract.contractEntry.TABLE_NAME,
                    new String[] {
                            movieDataContract.contractEntry.ID,
                            movieDataContract.contractEntry.IMG_PATH},null,null,null,null,null);
            if(cursor.getCount() == 0) {
                Toast.makeText(this, "there are no favorite movies yet!",Toast.LENGTH_SHORT).show();
            } else {
                sorterString = "favorite";
                showFavoriteFragment();
            }
            database.close();
            helper.close();
            cursor.close();
        }
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void showFavoriteFragment() {
    favoriteMoviesDetailsFragment fragment = new favoriteMoviesDetailsFragment();
    try {
        getFragmentManager().beginTransaction().replace(R.id.activity_container,fragment).commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    outState.putString("SORTER", sorterString);
    outState.putInt("POSITION",movieData.movieDataPosition);
    super.onSaveInstanceState(outState, outPersistentState);
}

public void movieRequest() {
        urlFinal = urlBase + sorterString + movieData.apiKey;
        urlFinal.trim();
        requestQueue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlFinal, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray array = response.getJSONArray("results");
                    movieData.movieDataArray = new movieData[array.length()];

                    for (int i = 0; i < array.length(); i++) {
                        movieData movie = new movieData();
                        JSONObject jsonObject = array.getJSONObject(i);
                        //movie.setPosition(i);
                        movie.setMovieId(jsonObject.getString("id"));
                        movie.setMovieImagePath(jsonObject.getString("poster_path"));
                        movie.setMovieTitle(jsonObject.getString("original_title"));
                        movie.setMoviePlot(jsonObject.getString("overview"));
                        movie.setMovieVoting(jsonObject.getString("vote_average"));
                        movie.setMovieReleaseDate(jsonObject.getString("release_date"));
                        movieData.movieDataArray[i] = movie;
                    }

                    MovieGridFragment gridFragment = new MovieGridFragment();
                    gridFragment.movieDataList = Arrays.asList(movieData.movieDataArray); //hier wird datalist eigentlich zugewiesen
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.activity_container, gridFragment);
                    try {
                        transaction.commitAllowingStateLoss();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("volley", String.valueOf(error));
            }
        }
        );
        requestQueue.add(jsonObjectRequest);

}

@Override
public void clickOnItem(int id) {
    movieData.movieDataPosition = id;
    if(movieData.movieDataArray == null) {
        movieRequest();
    } else {
        Intent intent = new Intent(this, detailsActivity.class);
        intent.putExtra("FRAGMENT","MOVIE");
        startActivity(intent);
    }

}

@Override
public void favoriteMovieItem(int movieId) {
    movieData.dbPosition = movieId;

    Intent intent = new Intent(this,detailsActivity.class);
    intent.putExtra("FRAGMENT","favorite");
    startActivity(intent);
} }
like image 735
Torben Avatar asked Mar 08 '17 17:03

Torben


2 Answers

You can try this

this.clickListener = (MainActivity) getActivity();

You get the FragmentActivity and cast it into your MainActivity

EDIT:

I suggest you to add a function in your fragment like :

public void setListener(clickInterfaceHelper listener) {
     this.clickListener = listener;
}

And in your activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(savedInstanceState == null) {
    MovieGridFragment fragment = new MovieGridFragment();
    fragment.setListener(this);   // some change here
                getSupportFragmentManager().beginTransaction()
                .add(R.id.activity_container, fragment)
                .commit();
        movieData.movieDataPosition = 0;
    }
    if(savedInstanceState != null) {
        sorterString = savedInstanceState.getString("SORTER");
    }

    if(savedInstanceState == null)
        movieData.movieDataPosition = 0;

    if(sorterString==null)
        sorterString="popular?";
    if(sorterString!="favorite" && sorterString!=null) {
        if(networkChecker.isNetworkAvailableChecker(this)) {
            movieRequest();
        }
    }

}

.... no relevant functions 


public void movieRequest() {
        urlFinal = urlBase + sorterString + movieData.apiKey;
        urlFinal.trim();
        requestQueue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlFinal, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray array = response.getJSONArray("results");
                    movieData.movieDataArray = new movieData[array.length()];

                    for (int i = 0; i < array.length(); i++) {
                        movieData movie = new movieData();
                        JSONObject jsonObject = array.getJSONObject(i);
                        //movie.setPosition(i);
                        movie.setMovieId(jsonObject.getString("id"));
                        movie.setMovieImagePath(jsonObject.getString("poster_path"));
                        movie.setMovieTitle(jsonObject.getString("original_title"));
                        movie.setMoviePlot(jsonObject.getString("overview"));
                        movie.setMovieVoting(jsonObject.getString("vote_average"));
                        movie.setMovieReleaseDate(jsonObject.getString("release_date"));
                        movieData.movieDataArray[i] = movie;
                    }

                    MovieGridFragment gridFragment = new MovieGridFragment();
                    gridfragment.setListener(this);  // some change here
                    gridFragment.movieDataList = Arrays.asList(movieData.movieDataArray); //hier wird datalist eigentlich zugewiesen
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.activity_container, gridFragment);
                    try {
                        transaction.commitAllowingStateLoss();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("volley", String.valueOf(error));
            }
        }
        );
        requestQueue.add(jsonObjectRequest);

}

Hope this helps.

like image 83
Cochi Avatar answered Nov 15 '22 12:11

Cochi


The context passed in is not necessarily your activity. It could be a wrapper around it with various theming and other overrides. You can never assume that when passed a Context, you're passed an Activity. If you need a reference to a click handler, write a setClickHandler function and call it explicitly.

Even if it is the Activity, the app doesn't know that- at that point its a Context. You'd need to explicitly cast it, which may not work (and may work on some versions of the OS and not others) due to paragraph 1.

like image 27
Gabe Sechan Avatar answered Nov 15 '22 11:11

Gabe Sechan