0

I am trying to create 8 buttons in one activity that will open their own activity. What am I missing? I am using Android Studio 1.1.0.

I get this error

java.lang.IllegalStateException: 
    Could not find a method AppleActivity(View) in the activity class 
    com.hashmi.omar.vodafonemobilephoneshop.Picker for onClick handler 
    on view class android.widget.Button with id 'button2'

Below is the Picker.class:

package com.hashmi.omar.vodafonemobilephoneshop;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.hashmi.omar.vodafonemobilephoneshop.util.SamsungActivity;

public class Picker extends Activity implements View.OnClickListener {

    Button button2, button3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picker);
        //Sets the font to Vodafone Light
        Typeface vodaLt = Typeface.createFromAsset(getAssets(), "VODAFONELT.TTF");
        TextView vodaHeading = (TextView)findViewById(R.id.textView4);
        vodaHeading.setTypeface(vodaLt);
        //Sets up a button
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(this);
    }

    private void buttonapple(){
        startActivity(new Intent(Picker.this, AppleActivity.class));
    }
    private void buttonsam(){
        startActivity(new Intent(Picker.this, SamsungActivity.class));
    }

    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.button2:
                buttonapple();
                break;
            case R.id.button3:
                buttonsam();
                break;

        }
    }

}

Below is the activity_picker.xml:

<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.hashmi.omar.vodafonemobilephoneshop.Picker"
android:background="#ffffffff">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Please choose a brand"
    android:id="@+id/textView4"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textColor="#ffff0000"
    android:textSize="31dp" />

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/textView4"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button
            android:layout_width="160dp"
            android:layout_height="125dp"

                android:background="@drawable/appsel"
            android:id="@+id/button2"
            android:layout_column="0"
            android:onClick="@string/title_activity_apple" />

        <Button
            android:layout_width="160dp"
            android:layout_height="125dp"

                android:background="@drawable/samselect"
            android:id="@+id/button3"
            android:layout_column="20"
            android:onClick="@string/title_activity_samsung" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"></TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button
            android:layout_width="160dp"
            android:layout_height="125dp"

                android:background="@drawable/sonyselect"
            android:id="@+id/button4"
            android:layout_column="0" />

        <Button
            android:layout_width="160dp"
            android:layout_height="125dp"

                android:background="@drawable/htcselect"
            android:id="@+id/button5"
            android:layout_column="20" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"></TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button
            android:layout_width="160dp"
            android:layout_height="125dp"

                android:background="@drawable/bbselect"
            android:id="@+id/button6"
            android:layout_column="0" />

        <Button
            android:layout_width="160dp"
            android:layout_height="125dp"

                android:background="@drawable/nokiaselect"
            android:id="@+id/button7"
            android:layout_column="20" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"></TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button
            android:layout_width="160dp"
            android:layout_height="125dp"

                android:background="@drawable/nexselect"
            android:id="@+id/button8"
            android:layout_column="0" />

        <Button
            android:layout_width="160dp"
            android:layout_height="125dp"

                android:background="@drawable/lgselect"
            android:id="@+id/button9"
            android:layout_column="20" />
    </TableRow>
</TableLayout>

Here is AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".HomeScreen"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Picker"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.hashmi.omar.vodafonemobilephoneshop.Picker" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".AppleActivity"
        android:label="@string/title_activity_apple"
        android:parentActivityName=".Picker" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.hashmi.omar.vodafonemobilephoneshop.Picker" />

        <action android:name="com.hashmi.omar.vodafonemobilephoneshop.AppleActivity" />
    </activity>
    <activity
        android:name=".SamsungActivity"
        android:label="@string/title_activity_samsung"
        android:parentActivityName=".Picker" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.hashmi.omar.vodafonemobilephoneshop.Picker" />
    </activity>
    <activity
        android:name=".util.SamsungActivity"
        android:label="@string/title_activity_samsung"
        android:parentActivityName=".Picker" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.hashmi.omar.vodafonemobilephoneshop.Picker" />
    </activity>
</application>

John
  • 3,673
  • 5
  • 29
  • 46
howzat
  • 59
  • 1
  • 2
  • 11

3 Answers3

0

Remove the simply onClick form your button2 because you are implemententing View.OnClickListener.

 <Button
        android:layout_width="160dp"
        android:layout_height="125dp"
        android:background="@drawable/appsel"
        android:id="@+id/button2"
        android:layout_column="0"
        />
Prashant Bhoir
  • 885
  • 6
  • 8
0

You have written this:

<Button
        ... other stuff here ...
        android:onClick="@string/title_activity_apple" />

If you use @string/title_activity_apple as the value of an attribute, Android will look up the string, and act as if that string's value was the actual value. So if the string has the value "AppleActivity", then it's equivalent to writing android:onClick="AppleActivity".

Next: what does setting android:onClick do? It tells the button the name of a method to call on the current activity when the button is clicked - see here. So you have told the button to call the method AppleActivity in the current activity when the button is pressed. (As you can also see on that page, it also expects to call a method with one parameter of type View - the arguments matter too)

Your activity class (the one containing the button0 does not have a method called AppleActivity which takes a View parameter. So the button can't find the method you told it to call, so it throws an exception (which crashes the app). This is exactly what the exception message said - the button could not find a method AppleActivity(View) in the activity class com.hashmi.omar.vodafonemobilephoneshop.Picker, which it was looking for because of your onClick attribute, on a android.widget.Button with id 'button2'.

user253751
  • 50,383
  • 6
  • 45
  • 81
-1
You need to create Method named in your xml file in your activity class as well. instead of creating following method

 private void buttonapple(){
    startActivity(new Intent(Picker.this, AppleActivity.class));
}

 private void title_activity_apple(){
    startActivity(new Intent(Picker.this, AppleActivity.class));
}

After above change check again.

Silvans Solanki
  • 1,261
  • 1
  • 14
  • 27