<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 这里搜索查看是customFrameLayout.I在我的主要活动的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上的基本活动的回调。你可以清楚地看到实现它的类,如果我已经在BaseActivity中实现了回调,那么我根本不需要投入BaseActivity。只需通过“this”关键字即可获得。从xml文件创建的视图可以转换为托管它的活动。几乎每个演员都是成功的。但有了这个,我不知道这里是什么情况。 –
是的,我知道你......“......根本不用投身于BaseActivity ..”,这就是为什么我首先根据自己的代码意义来提出它的原因。无论如何,将从视图中获得的“Context”投射到“Activity”当然不是您所见的安全代码。我所说的方法是'KeyListener'对象的'onClicked()',因为某种原因需要一个'BaseActivity'实例(因为您已经通过构造函数传入),因此我的推理。 – bitbybit
看起来像你必须改变你的代码不依赖于铸造(为什么需要它?),或者你可以在你的'SearchView'构造函数中记录堆栈跟踪,并查看'LayoutInflater'用于你的'context'的底部视图。 – bitbybit