【发布时间】:2015-05-28 20:00:18
【问题描述】:
在下面的类中,enableAssistedSearch 方法中出现以下三个编译器错误。
无法对非静态方法进行静态引用
getSystemService(String)来自Activity类型无法对非静态方法进行静态引用
getComponentName()来自Activity类型无法对非静态方法进行静态引用
findViewById(int)来自Activity类型
我正在使用Eclipse,“清理项目”没有帮助。
此外,该类还包含另一个findViewById 调用,但这不会引发任何错误,这很令人困惑。
package practiceTests.test2.assistedSearchUsingSearchWidget;
import android.app.Activity;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.widget.TextView;
public class SearchActivity extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
textView = (TextView) findViewById(R.id.searchActivity_textView);
enableAssistedSearch();
handleIntent(getIntent());
}
/**
*
*/
private static void enableAssistedSearch() {
// SearchManager => provides access to the system search services.
// Context.getSystemService() => Return the handle to a system-level
// service by name. The class of the returned object varies by the
// requested name.
// Context.SEARCH_SERVICE => Returns a SearchManager for handling search
// Context = Interface to global information about an application
// environment. This is an abstract class whose implementation is
// provided by the Android system. It allows access to
// application-specific resources and classes, as well as up-calls for
// application-level operations such as launching activities,
// broadcasting and receiving intents, etc.
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);//**************************
//getSearchableInfo() => gets information about a searchable activity. @return => the activity to get searchable information for.
SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());//**************************
SearchView searchView = (SearchView) findViewById(R.id.searchView);//**************************
}
/**
*
*/
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
/**
*
*/
private void handleIntent(Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SEARCH)) {
performSearch(intent.getStringExtra(SearchManager.QUERY));
}
}
/**
*
*/
private void performSearch(String searchQuery) {
textView.setText(searchQuery);
}
}
我不知道为什么!那么有人可以指出任何问题,或者建议我应该怎么做吗?
【问题讨论】:
标签: java android android-activity compiler-errors