<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 这里的searchView是customFrameLayout。我在我的主要 Activity xml文件中使用了这个xml文件。
<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>
这是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();
}
}
在我的 MainActivity 中
setContentView(R.layout.activity_main);
java.lang.ClassCastException:android.view.ContextThemeWrapper 无法转换为 com.ujjwal.univhub.BaseActivity
我不能将上下文转换到托管 View 的 Activity 。
最佳答案
ClassCastException
只是意味着该对象不是您尝试转换的类型
(或者不能安全地表现为类型
)。
This可能有帮助。
如果您使用构造函数new SearchView(yourBaseActivityContext)
以编程方式创建提供显式上下文(您的BaseActivity
)的 View ,则不应引发此异常。但是,从 xml 来看,我不确定 LayoutInflater
是否使用您的 Activity 上下文或更通用的上下文来构建 View 。
编辑:(回应op的评论)
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.
在您的BaseActivity
中,创建一个界面:
public interface KeyClickListener { void clicked(SomeUiData data) }
向您的 SearchView
类添加一个方法来注册可以收到通知的监听器(例如您的 BaseActivity
)
private BaseActivity.KeyClickListener mListener;
public void setKeyClickListener(BaseActivity.KeyClickListener listener) {
mListener = listener;
}
在 BaseActivity
的 onCreate()
中(以便在执行任何用户点击之前注册监听器):
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
}
});
因此,实际上,现在您拥有一个在 BaseActivity
中声明的监听器,它可以执行任何 UI 更新操作,而无需 KeyListener
需要 BaseActivity
实例。将 mListener
传递给您的 KeyListener
并在原始的 KeyListener.onClicked()
方法中,您可以使用可以更新 UI 的 click/ui 数据调用在 KeyClickListener
中声明的任何方法(因为它是在 Activity 中声明的)。
关于java - 无法将 View.getContext 强制转换为托管 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39333856/