Java 错误且不知道原因:(
我一直在试图找出如何制作一个简单的搜索按钮。我发现了这个()我认为会有帮助,我想它是有帮助的,我只是不明白为什么这里有一个错误: SearchableActivity.javaJava 错误且不知道原因:(,java,android,xml,Java,Android,Xml,我一直在试图找出如何制作一个简单的搜索按钮。我发现了这个()我认为会有帮助,我想它是有帮助的,我只是不明白为什么这里有一个错误: SearchableActivity.java package com.ryan.buttonsimple; import android.app.SearchManager; import android.content.Context; import android.support.v7.app.ActionBarActivity; import android
package com.ryan.buttonsimple;
import android.app.SearchManager;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
public class SearchableActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) findItemById(R.id.search_view); //**HERE IS THE ERROR! "findItemById" and "search_view" "Cannot resolve method for both**
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_searchable, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的searchable.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint">
</searchable>
这是我的AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ryan.buttonsimple" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SearchableActivity"
android:label="@string/title_activity_searchable" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>
</manifest>
如果您在SearchableActivity.java部分中查看,则行SearchView SearchView=(SearchView)findItemById(R.id.search\u视图);它说无法解析方法?您没有声明它,ActionBarActivity也没有声明它,那么它为什么要解析呢?SearchView通常被声明为菜单/name.xml的一部分,在onCreateOptions菜单中膨胀。如果在onCreateOptions菜单
之前调用onCreate,您认为会发生什么?您可能想findViewById…您的IDE不建议您这样做吗?请从logcat发布堆栈跟踪。这会准确地告诉您错误是什么。如果没有,我们只是猜测。