简体   繁体   English

Android意向未启动活动

[英]Android intent not launching activity

After attempting to perform a search and failing, I am trying to launch an activity to display results from a search, but the app crashes when it gets to the point of the intent: 尝试执行搜索并失败后,我试图启动一个活动以显示搜索结果,但是当应用程序到达意图点时,它崩溃了:

if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
    String query = intent.getStringExtra(SearchManager.QUERY);
    Intent mapIntent = new Intent(SearchActivity.this, MapResults.class);
    mapIntent.putExtra("query", query);
    startActivity(mapIntent);
}

here is the class it is supposed to launch: 这是应该启动的类:

public class MapResults extends MapActivity implements OnGestureListener, OnDoubleTapListener {
/** Called when the activity is first created. */

public String query;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

        Intent intent = getIntent();
        query = intent.getStringExtra("query");

    Toast.makeText(this, "The query: " + query, Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean isRouteDisplayed() {
        return false;
    }

and the manifest file: 和清单文件:

<activity android:name=".SplashScreen"
    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=".Main">
    <intent-filter>
        <action android:name="com.example.android.test.Main" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <meta-data android:name="android.app.default_searchable"
               android:value=".SearchActivity" />
</activity>
<activity android:name=".SearchActivity" 
    android:launchMode="singleTop" >
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data android:name="android.app.default_searchable"
        android:value=".SearchActivity" />
    <meta-data android:name="android.app.searchable"
        android:resource="@layout/searchable"/>
</activity>
<activity android:name=".MapResults">
    <intent-filter>
        <action android:name="com.example.android.test.MapResults"       
            android:label="@string/map_results_title"/>
    </intent-filter>
    <meta-data android:name="android.app.default_searchable"
        android:value=".SearchActivity" />
</activity>

I know that in the MapResults class it is not launching a map at the moment, its just displaying text, but this is just while I am trying to receive the data from the intent. 我知道在MapResults类中,它目前不启动地图,它只是显示文本,但这只是在我试图从意图中接收数据时。

Any idea, cause I'm stuck! 任何想法,因为我被卡住了!

<intent-filter>
    <action android:name="com.example.android.test.Main" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

can you change DEFAULT to LAUNCHER? 您可以将DEFAULT更改为LAUNCHER吗? just try it 去尝试一下

In your MapResults class, the line : 在您的MapResults类中,该行:

Toast.makeText(this, "The query: " + query, Toast.LENGTH_LONG).show();

will create a NullPointerException as you are concatenating a null String. 当您串联空字符串时,将创建NullPointerException。 Your data member query is null at this point, you should have something like this : 您的数据成员查询此时为null,您应该具有以下内容:

query = intent.getStringExtra( "query" );
Toast.makeText(this, "The query: " + query, Toast.LENGTH_LONG).show();

Otherwise, your mechanism to transfer data using intents seems alright to me. 否则,您使用意图传输数据的机制在我看来似乎还不错。

Regards, Stéphane 问候,斯特凡

The only problem, that I can see in the code is your intent variable is null, so it crashes on intent.getAction() 我在代码中看到的唯一问题是您的intent变量为null,因此在intent.getAction()上崩溃

up: haven't noticed MapActivity code - if variable query is not initialized at the moment of Toast - yes, it's a second problem 上:尚未注意到MapActivity代码-如果在Toast时未初始化变量查询 -是的,这是第二个问题

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

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