Android Intent 应用程序崩溃

Android Intent Application Crashing

我正在尝试为项目创建一个非常小的应用程序。我有两个按钮可以调用具有隐式意图的应用程序,但每当我按下其中一个时,应用程序(我正在创建的那个)就会崩溃。这是怎么回事?这是代码:

package com.messengerassist;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void startPostmates() {
        Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.postmates.android.courier");
        startActivity(launchIntent);
    }
    public void startCaviar() {
        Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.trycaviar.courier");
        startActivity(launchIntent);
    }
}

然后

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.messengerassist.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Postmates"
        android:id="@+id/startPostmates"
        android:onClick="startPostmates"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Caviar"
        android:id="@+id/startCaviar"
        android:onClick="Start Caviar"
        android:layout_below="@+id/startPostmates"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

这是 logcat 给我的:

12-21 20:10:28.919 4108-4108/com.messengerassist E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.messengerassist, PID: 4108
                                                                   java.lang.IllegalStateException: Could not find method startPostmates(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'startPostmates'
                                                                       at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:307)
                                                                       at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:266)
                                                                       at android.view.View.performClick(View.java:4789)
                                                                       at android.view.View$PerformClick.run(View.java:19881)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:135)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

当您在 XML 中注册点击侦听器方法时,该方法应将 View 作为其参数。从 logcat 像这样更改方法签名可能会解决问题

 public void startPostmates(View view) { ...}
 public void startCaviar(View view) {...}