1

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="@drawable/window_foreground">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
        app:elevation="@dimen/appbar_resting"
    android:theme="@style/AppTheme.NoActionBar">

        <com.ujjwal.univhub.components.SearchView
            android:id="@+id/search_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.design.widget.AppBarLayout>

<include layout="@layout/fragment_university_lsit"></include>

</FrameLayout>

app_bar_main.xml Here searchView is customFrameLayout.I used this xml file in my main activity xml file.

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

    <include
        layout="@layout/floating_action_group"/>
</FrameLayout>

This is activity_main.xml

public class SearchView extends FrameLayout implements AdapterView.OnItemClickListener{


 @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


    SuggestionAdapter.ViewHolder viewHolder = (SuggestionAdapter.ViewHolder)view.getTag();
    University selectedUniversity = viewHolder.getUniversity();
    String sendData = FilterBuilder.createCodeFilter(selectedUniversity.getCode()).toJson();
    //may occure error here
    Log.d("serach by name", "onItemClick: error");
    KeyListener listener = new KeyListener((BaseActivity)getContext());//error thrown here.
    listener.onClicked(sendData, Properties.LOCALHOST + Properties.UNIVERSITY_CODE);
    queryInput.setText(selectedUniversity.getName());
    suggestionAdapter.clear();
    suggestionAdapter.notifyDataSetChanged();

    }

}    

In my MainActivity

   setContentView(R.layout.activity_main);

java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to com.ujjwal.univhub.BaseActivity

My can't i cast context to the activity hosting the view.

ujjwal mainali
  • 379
  • 1
  • 5
  • 17

1 Answers1

0

ClassCastException simply means the object isn't of the type (or can't safely behave as the type) you are trying to cast into. This might help.

If you create the view programatically providing an explicit context (your BaseActivity) using constructor new SearchView(yourBaseActivityContext), this exception should not be thrown. However, from xml, I'm not sure if the LayoutInflater uses your activity context or a more generic context for constructing the view.

Edit: (in response to op's comment)

i need it to post the data in UI through the handler of baseActivity. KeyListener will have to get access to BaseActivity instance. If i have to make keylistener independent of activity then its will require a complete turn over of the implementation.

In your BaseActivity, make an interface:

public interface KeyClickListener { void clicked(SomeUiData data) }

add a method to your SearchView class to register a listener (e.g. your BaseActivity) which can be notified

private BaseActivity.KeyClickListener mListener;
public void setKeyClickListener(BaseActivity.KeyClickListener listener) {
    mListener = listener;
}

in your BaseActivity's onCreate() (so that listener is registered before any user clicks can be performed):

mSearchView = findViewById... ;// get your SearchView instance
mSearchView.setKeyclickListener(new KeyClickListener() {
    void clicked(SomeUiData newUiInfo) {
        // update your UI using newUiInfo here
        // use any handler accessible in your BaseActivity
    }
});

So, effectively, now you have a listener which is declared in your BaseActivity which can perform any UI update actions without KeyListener needing BaseActivity instance. Pass mListener to your KeyListener and in your original KeyListener.onClicked() method, you could invoke any methods you declared in KeyClickListener with the click/ui data which can update the UI (because it is declared in the activity).

Community
  • 1
  • 1
bitbybit
  • 162
  • 1
  • 7
  • that's not the callback on the baseactivity its on View. You can clearly see the class implementing it,if i had implemented callback in BaseActivity,i wouldn't have to cast at all to get BaseActivity.Just could simply get by "this" keyword. View created from xml file can be cast to the activity hosting it. Almost every cast is successful. But with this i don't know what is the case here. – ujjwal mainali Sep 05 '16 at 19:05
  • Yes I know you _"...wouldn't have to cast at all to get BaseActivity.."_, which is why I suggested it in the first place based on what sense I could make of your code. Anyway, casting a `Context` obtained from a view to `Activity` is certainly not safe code as you have seen. The method I was talking about was `onClicked()` of the `KeyListener` object, which for some reason needs a `BaseActivity` instance (as you have passed in constructor by casting), hence my inference. – bitbybit Sep 05 '16 at 19:50
  • Looks like you have to change you code to not depend on the casting (why need it?) or you could log the stacktrace in your `SearchView` constructor and get to the bottom of what `context` the `LayoutInflater` used for your view. – bitbybit Sep 05 '16 at 19:58
  • i need it to post the data in UI through the handler of baseActivity.KeyListener will have to get access to BaseActivity instance. If i have to make keylistener independent of activity then its will require a complete turn over of the implementation. – ujjwal mainali Sep 06 '16 at 06:30
  • Updated the answer. See if this helps. – bitbybit Sep 06 '16 at 07:32