Android Intent应用程序崩溃

时间:2015-12-22 04:10:09

标签: java android android-intent android-implicit-intent

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

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)

1 个答案:

答案 0 :(得分:4)

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

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