Android findViewById()、getComponentName() 和 getSystemService():无法对类型 Activity 中的非静态字段进行静态引用

Android findViewById(), getComponentName() and getSystemService(): Cannot make a static reference to a nonstatic field from type Activity

在下面的 class 中,我在 enableAssistedSearch 方法中遇到以下三个编译器错误。

我正在使用 Eclipse,"cleaning the project" 没有帮助。

此外,class 还包含另一个 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 的方法,但是你不能访问实例本身,你会得到这个编译时错误,等等其他错误

这是您声明的内容:

private static void enableAssistedSearch()

你应该尝试删除这个词 "static"

getSystemService(String)、getComponentName()、findViewById(int)定义为继承自superclassActivity(甚至baseclassContext)的成员方法.成员方法是对象实例的一部分,它坚持处理自己的某些事情,并暗示在创建对象实例后调用。

静态方法甚至可以在创建任何对象实例之前在静态范围内调用。在此期间,您可以假设不存在 Activity 实例,因此将无法调用这 3 个成员方法。

请注意,您通常不会创建 Activity 的新实例,但 android sdk 会创建它。