行动搜索没有开始新的活动

时间:2016-02-04 08:56:28

标签: java android

我在两个不同的文件中有两个类,一个是MainActivity,另一个是SearchResultsActivity。

出于某种原因,当我按下搜索按钮时,它没有启动SearchResultsActivity,我假设这是因为我放了几个断点(甚至一些在课程的开头)并且调试器没有'似乎达到了他们。

MainActivity.Java

package myapp.myapp;

public class MainActivity extends ActionBarActivity {

    private Toolbar toolbar;
    ProgressBar theProgressBar;
    TextView stInstTxt;


    public void sendMessage(View view) {
        Intent intent = new Intent(this, MainActivity.class);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);

        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search_element).getActionView();
        // Assumes current activity is the searchable activity
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);

        return true;

    }

    @Override
    public boolean onOptionsItemSelected (MenuItem item){
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.search_element:
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

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

        //text view
        stInstTxt = (TextView) findViewById(R.id.textView01);

        //progress bar
        theProgressBar = (ProgressBar) findViewById(R.id.progressBar01);
        theProgressBar.setVisibility(View.INVISIBLE);

        //toolbar
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);


    }


}

SearchResultsActivity.Java

package myapp.myapp;

public class SearchResultsActivity extends Activity{

    TextView stInstTxt;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = getApplicationContext();
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            searchItems(query);
        }
    }
    public void searchItems(String query) {
       stInstTxt.append(query);
    }
}

的AndroidManifest.xml

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">


        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">


            <intent-filter>

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

        <activity android:name="myapp.myapp.SearchResultsActivity"
            android:label="@string/app_name">

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

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

        </activity>


    </application>

</manifest>

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#FFF9C4">

        <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar"
            />

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/progressBar01"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

        <RelativeLayout
            android:id="@+id/InnerRelativeLayout"
            android:layout_width="wrap_content"
            android:paddingTop="70dp"
            android:layout_height="wrap_content">

            <TextView android:text="@string/help_text_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView01"
                android:scrollbars="vertical"
                android:visibility="visible"/>

        </RelativeLayout>

</RelativeLayout>

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/search_element"
        android:orderInCategory="200"
        android:title="@string/search_hint"
        android:icon="@drawable/ic_search"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="android.support.v7.widget.SearchView">


    </item>

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />

</menu>

AndroidManifest.xml会出现问题吗?

1 个答案:

答案 0 :(得分:0)

经过近6个小时的尝试实现(从android documentation as a guide开始),并尝试了几种不同的方法来做到这一点,我觉得我可能会帮助其他有类似问题的人省一些时间,我来到this tutorial

我读了所有内容,但我最终做的是下载代码并将其与我的相比,直到我发现我错过了一些重要的事情。官方的android文档是不完整的,而且几乎没有办法在没有看到真实完整的工作示例的情况下提出解决方案。