【问题标题】:Android findViewById(), getComponentName() and getSystemService(): Cannot make a static reference to a nonstatic field from type ActivityAndroid findViewById()、getComponentName() 和 getSystemService():无法对 Activity 类型的非静态字段进行静态引用
【发布时间】: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


    【解决方案1】:

    因为你的方法是 static 并且它不允许访问对象实例本身,并且由于 findViewByIdActivity 的方法,但是你不能访问实例本身,你得到这个编译时错误,其他错误以此类推

    【讨论】:

    • 谢谢,这解决了问题。我已经开始阅读基本的 Java 教程来修复这些概念。再次感谢您。
    【解决方案2】:

    这是您声明的内容:

    私有静态 void enableAssistedSearch()

    您应该尝试删除“静态”一词

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 2015-11-30
      • 2021-11-04
      • 1970-01-01
      相关资源
      最近更新 更多