gpt4 book ai didi

java - Android Intent 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 18:08:49 24 4
gpt4 key购买 nike

我正在尝试为项目创建一个非常小的应用程序。我有两个按钮可以调用带有隐式 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)

最佳答案

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

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

关于java - Android Intent 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34408296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com