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:
<?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:
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. From the logcat changing the method signature like this may solve the problem
public void startPostmates(View view) { ...}
public void startCaviar(View view) {...}