Android Studio - 图片按钮在点击后崩溃了

时间:2016-01-23 14:25:57

标签: java android button crash

我正在尝试制作一些简单的图像点击按钮进入另一项活动,但我无法让它工作。

它说:

Unfortunately, myapp has stopped.

如果我突出显示我的android:onClick="myButton",则会显示Method myButton' is missing in 'MainActivity' in my layout.xml

MainActivity.java

package com.zemoapps.alarmclockgold;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


//Old code "public class MainActivity extends AppCompatActivity {"
public class MainActivity extends AppCompatActivity {

    private Button myButton;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        TextView myTextview = (TextView) findViewById(R.id.RepeatingText);
        TextView myTextview2 = (TextView) findViewById(R.id.CountDownText);
        Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/AndadaRegular.otf");
        myTextview.setTypeface(myCustomFont);
        myTextview2.setTypeface(myCustomFont);


    }

    public class MyCustomTextView extends TextView {

        public MyCustomTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.setTypeface(Typeface.createFromAsset(context.getAssets(),
                    "fonts/AndadaRegular.otf"));
        }

    }

    public class AlarmPage extends AppCompatActivity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            myButton = (Button) findViewById(R.id.RepeatingImage);

            myButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    startActivity(new Intent(MainActivity.this, AddAlarm.class));

                }
            });


        }


    }}

Layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:andriod="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/BG"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9e84d3"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zemoapps.onething.MainActivity">

<ImageButton
    android:id="@+id/RepeatingImage"
    android:layout_width="200dp"
    android:layout_height="150dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="#00000000"
    android:contentDescription="@null"
    android:onClick="myButton"
    android:scaleType="fitCenter"
    android:src="@drawable/alarm1" />

</RelativeLayout>

AndroidManifest说也有错误: enter image description here

java.lang.IllegalStateException: Could not find method myButton(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageButton with id 'RepeatingImage'
                                                                                 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:4084)
                                                                                 at android.view.View$PerformClick.run(View.java:16966)
                                                                                 at android.os.Handler.handleCallback(Handler.java:615)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                                 at android.os.Looper.loop(Looper.java:137)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                                 at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:1)

从XML中的ImageButton中删除android:onClick =“myButton”。

<ImageButton
    android:id="@+id/RepeatingImage"
    android:layout_width="200dp"
    android:layout_height="150dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="#00000000"
    android:contentDescription="@null"
    android:onClick="myButton" // remove this line from your xml
    android:scaleType="fitCenter"
    android:src="@drawable/alarm1" />

答案 1 :(得分:1)

这是因为您扩展了AppCompatActivity。 如果扩展Activity,它将正常工作。

答案 2 :(得分:0)

您没有为图片按钮点击定义方法,您应该将其添加到MainActivity,如下所示:

public void myButton(View view){
}

答案 3 :(得分:0)

从ImageButton节点中删除下一行:

android:onClick="myButton"

当你添加它时,Android会在你的Activity中找到一个名为“myButton”的方法。