如何修复以下类无法在android中实例化
我正在创建一个应用程序,它由从中下载的操作栏上的搜索小部件组成,我已将其放置在my layout.xml中,但它在名为 无法实例化以下类:无法实例化以下类: -com.millan.searchmenu.persistentsearch.SearchBox(打开类,显示错误日志) 有关详细信息,请参阅错误日志(窗口>显示视图)。 提示:在自定义视图中使用View.isInEditMode()可以在Eclipse中显示时跳过代码 java.lang.NullPointerException通过运行应用程序也会发生运行时null指针异常,有人能告诉我如何修复此问题: 这是我的活动:如何修复以下类无法在android中实例化,android,android-menu,android-search,Android,Android Menu,Android Search,我正在创建一个应用程序,它由从中下载的操作栏上的搜索小部件组成,我已将其放置在my layout.xml中,但它在名为 无法实例化以下类:无法实例化以下类: -com.millan.searchmenu.persistentsearch.SearchBox(打开类,显示错误日志) 有关详细信息,请参阅错误日志(窗口>显示视图)。 提示:在自定义视图中使用View.isInEditMode()可以在Eclipse中显示时跳过代码 java.lang.NullPointerException通过运行
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.manual);
ActionBar actionbar;
actionbar = getActionBar();
search = (SearchBox) findViewById(R.id.searchbox);
search.enableVoiceRecognition(this);
for(int x = 0; x < 10; x++){
com.milan.searchmenu.persistentsearch.SearchResult option = new SearchResult("Result " + Integer.toString(x), getResources().getDrawable(R.drawable.ic_history));
search.addSearchable(option);
}
search.setMenuListener(new MenuListener(){
@Override
public void onMenuClick() {
//Hamburger has been clicked
Toast.makeText(Manual.this, "Menu click", Toast.LENGTH_LONG).show();
}
});
search.setSearchListener(new SearchListener() {
@Override
public void onSearchTermChanged() {
// TODO Auto-generated method stub
}
@Override
public void onSearchOpened() {
// TODO Auto-generated method stub
}
@Override
public void onSearchClosed() {
// TODO Auto-generated method stub
}
@Override
public void onSearchCleared() {
// TODO Auto-generated method stub
}
@Override
public void onSearch(String result) {
Toast.makeText(Manual.this, result +" Searched", Toast.LENGTH_LONG).show();
}
});
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.manual);
ActionBar ActionBar;
actionbar=getActionBar();
search=(SearchBox)findviewbyd(R.id.SearchBox);
搜索。启用语音识别(本);
对于(int x=0;x<10;x++){
com.milan.searchmenu.persistentsearch.SearchResult option=newsearchresult(“Result”+Integer.toString(x),getResources().getDrawable(R.drawable.ic_history));
search.addSearchable(可选);
}
search.setMenuListener(新的MenuListener(){
@凌驾
public void onMenuClick(){
//汉堡包被点击了
Toast.makeText(Manual.this,“菜单点击”,Toast.LENGTH_LONG.show();
}
});
search.setSearchListener(新的SearchListener(){
@凌驾
onSearchTermChanged()上的公共无效{
//TODO自动生成的方法存根
}
@凌驾
已删除的公共空白(){
//TODO自动生成的方法存根
}
@凌驾
搜索已关闭()上的公共无效{
//TODO自动生成的方法存根
}
@凌驾
已清除搜索上的公共无效(){
//TODO自动生成的方法存根
}
@凌驾
搜索上的公共void(字符串结果){
Toast.makeText(Manual.this,result+“Searched”,Toast.LENGTH_LONG.show();
}
});
这是我的XML:
<RelativeLayout
android:id="@+id/Relative_layout"
android:layout_width="wrap_content"
android:layout_height="51dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#673AB7">
<com.milan.searchmenu.persistentsearch.SearchBox
android:layout_width="wrap_content"
android:id="@+id/searchbox"
android:layout_height="wrap_content">
</com.milan.searchmenu.persistentsearch.SearchBox>
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/pager_sliding_tab_strip">
</android.support.v4.view.ViewPager>
<com.milan.tabs.pagerslidingstrip.PagerSlidingTabStrip
android:id="@+id/pager_sliding_tab_strip"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/Relative_layout"
android:textSize="15dp"
app:pstsShouldExpand="true"
app:pstsDividerColor="#B39DDB"
app:pstsIndicatorHeight="45dp"
android:background="#673AB7"
app:pstsIndicatorColor="#FFFFFF">
</com.milan.tabs.pagerslidingstrip.PagerSlidingTabStrip>
}
}
您需要将下载的库添加为模块的依赖项。在androidstudio中,执行以下操作
从帖子中我了解到,你在开发应用程序而不是运行时遇到了这个问题 如果问题是布局文件未在设计视图中加载,则在布局中使用的SearchBox控件将出现问题 com.millan.searchmenu.persistentsearch.SearchBox 在自定义视图中,在设计视图中渲染时,它无法完全工作。例如,如果自定义视图正在从资产等访问某些内容,则该依赖关系将仅在运行时解析,而不会在设计模式下解析,因此会出现此错误
因此,为了进行修复,您需要将这些代码放在if视图块中。isInEditMode()。我在eclipse中执行的操作dude@Disco对不起。我真的不记得在Eclipse中该如何做了。太长时间了:)想搬到安卓工作室吗?非常感谢你的回答,伙计,但我的项目目前正处于巅峰阶段,我不想冒险在这个时候迁移到android studio下一次我将在我能找到视图的地方挑战它。isInEdit模式dude@EldhoseMBabu@BujjiDeepu它是视图的静态功能。在onDraw方法内创建自定义视图时,需要使用该函数。此函数用于区分视图是在设计模式下加载还是在应用程序的实际运行中加载。在这种情况下,如果没有自定义视图的代码,则无法执行任何操作。