Android在活动类中找不到AppleActivity(View)方法

时间:2015-04-11 11:36:11

标签: java android android-studio

我正在尝试在一个活动中创建8个按钮,这些按钮将打开自己的活动。我错过了什么?我使用的是Android Studio 1.1.0。

我收到此错误

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'

下面是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;

        }
    }

}

下面是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>

这是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>

3 个答案:

答案 0 :(得分:0)

删除简单的onClick表单你的button2,因为你正在实现View.OnClickListener。

 <Button
        android:layout_width="160dp"
        android:layout_height="125dp"
        android:background="@drawable/appsel"
        android:id="@+id/button2"
        android:layout_column="0"
        />

答案 1 :(得分:0)

你写过:

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

如果您使用@string/title_activity_apple作为属性的值,Android会查找字符串,并表现为该字符串的值是实际值。因此,如果字符串的值为#34; AppleActivity&#34;,那么它等同于编写android:onClick="AppleActivity"

下一步:设置android:onClick有什么作用?它告诉按钮单击按钮时调用当前活动的方法的名称 - see here。因此,当按下按钮时,您已告诉按钮在当前活动中调用方法AppleActivity。 (正如您也可以在该页面上看到的那样,它还希望调用一个类型为View的参数的方法 - 参数也很重要)

您的活动类(包含button0的活动类没有名为AppleActivity的方法,该方法带有View参数。因此按钮无法找到您告诉它调用的方法,所以它抛出一个异常(它会使应用程序崩溃)。这正是异常消息所说的 - could not find a method AppleActivity(View) in the activity class com.hashmi.omar.vodafonemobilephoneshop.Picker因为onClick属性而在android.widget.Button with id 'button2'上寻找的按钮{{1}} }。

答案 2 :(得分:-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));
}

经过上述更改后再次检查。