简体   繁体   English

Android Intent应用程序崩溃

[英]Android Intent Application Crashing

I'm trying to create a very small application for a project. 我正在尝试为项目创建一个非常小的应用程序。 I have two buttons that call applications with implicit intents, but whenever I press either, the application (the one I'm creating) keeps crashing. 我有两个按钮以隐式意图调用应用程序,但是每当我按下其中一个按钮时,该应用程序(我正在创建的那个)都会崩溃。 What's going on? 这是怎么回事? Here's the code: 这是代码:

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);
    }
}

and then 接着

activity_main.xml: 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>

Here is what logcat is giving me: 这是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)

When you register a click listener method in your XML ,The method should take View as it's parameter. 当您在XML中注册点击侦听器方法时,该方法应将View作为其参数。 From the logcat changing the method signature like this may solve the problem 从logcat更改这样的方法签名可以解决问题

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

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

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