简体   繁体   English

动作SEARCH不会开始新的活动

[英]Action SEARCH doesn't start new Activity

I have two classes in two different files, one is MainActivity and the other one is SearchResultsActivity. 我在两个不同的文件中有两个类,一个是MainActivity,另一个是SearchResultsActivity。

For some reason when i press the search button it doesn't start SearchResultsActivity, I'm assuming this because i put several breakpoints (even some at the beginning of the class) and the debugger doesn't seem to reach them. 由于某些原因,当我按下搜索按钮时,它不会启动SearchResultsActivity,我之所以这样假设,是因为我放置了几个断点(甚至是在课程开始时的一些断点),并且调试器似乎无法到达它们。

MainActivity.Java 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 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 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 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_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>

Could there be something wrong with AndroidManifest.xml? AndroidManifest.xml可能有问题吗?

After almost 6 hours of trying to implement this (starting with the android documentation as a guide ), and trying dozens of different ways to do it, i feel i maybe could help someone else with a similar problem save some time, I came up to this tutorial 经过将近6个小时的尝试实现这一点(从android文档作为指导 ),并尝试了数十种不同的方法来实现这一点,我觉得我可能可以帮助其他遇到类似问题的人节省一些时间, 本教程

I read it all but what I ended up doing was downloading the code and comparing it with mine until i found that I was missing a couple of important things. 我读完了所有内容,但最终要做的是下载代码并将其与我的代码进行比较,直到发现缺少一些重要的东西为止。 The official android documentation is incomplete, and pretty much there was no way i could have came up with a solution without seeing a real and complete working example. 官方的android文档是不完整的,而且几乎没有办法,如果没有看到一个真实而完整的工作示例,我不可能想出一个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM