无法从静态上下文

时间:2016-06-01 12:42:21

标签: android android-studio

我正在关注如何从搜索视图中获取查询的YouTube教程,并且我无法从静态上下文中引用此错误。

MainActivity.java:

package com.zoggfrombetelgeuse.clef;

import android.app.SearchManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar my_toolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(my_toolbar);
    getSupportActionBar().setTitle(R.string.my_toolbar_tile);

    Intent searchIntent = getIntent();
    if(Intent.ACTION_SEARCH.equals(searchIntent.getAction())){

        String query = searchIntent.getStringExtra(SearchManager.QUERY);

        Toast.makeText(MainActivity.this, query, Toast.LENGTH_SHORT).show();
    }



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main_menu,menu);

    SearchView searchview = (SearchView) menu.findItem(R.id.action_search);
    SearchManager searchmanager = (SearchManager)       getSystemService(SEARCH_SERVICE);
    SearchView.setSearchableInfo(searchmanager.getSearchableInfo(getComponentName())  );

    return super.onCreateOptionsMenu(menu);
    }
}

MainActicity是我唯一的活动。

的Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zoggfrombetelgeuse.clef">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />

        </intent-filter>

        <meta-data
            android:name="android.app.default_searchable"
            android:value=".MainActivity"/>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable"
            />
    </activity>
</application>

1 个答案:

答案 0 :(得分:0)

更改此行

SearchView.setSearchableInfo(searchmanager.getSearchableInfo(getComponentName())  );

searchview.setSearchableInfo(searchmanager.getSearchableInfo(getComponentName())  );