gpt4 book ai didi

java - Android findViewById()、getComponentName() 和 getSystemService() : Cannot make a static reference to a nonstatic field from type Activity

转载 作者:行者123 更新时间:2023-11-29 20:44:29 24 4
gpt4 key购买 nike

在接下来的类(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 并且它不允许访问对象实例本身,并且因为findViewByIdActivity 的方法>,但是你不能访问实例本身,你会得到这个编译时错误,等等其他错误

关于java - Android findViewById()、getComponentName() 和 getSystemService() : Cannot make a static reference to a nonstatic field from type Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30516041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com