作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在接下来的类(class)中,我在 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);
}
}
我想不通为什么!那么任何人都可以指出任何问题,或者建议我应该做什么吗?
最佳答案
这是因为您的方法是static
并且它不允许访问对象实例本身,并且因为findViewById
是Activity
的方法>,但是你不能访问实例本身,你会得到这个编译时错误,等等其他错误
关于java - Android findViewById()、getComponentName() 和 getSystemService() : Cannot make a static reference to a nonstatic field from type Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30516041/
我是一名优秀的程序员,十分优秀!