Android - IllegalStateException:无法执行活动的方法,由InvocationTargetException引起

时间:2014-02-10 07:17:17

标签: android illegalstateexception numberformatexception invocationtargetexception

我正在参加Coursera Android课程,并且正在尝试完成我开始使用的应用程序。我试图以设定的间隔发生动画。 See here for more details

我查看了许多具有相同标题的帖子,但每个人似乎都有不同的解决方案。在保存和编译时,我在Eclipse中没有出现错误。

这是我的Logcat结果:

    02-09 22:56:10.811: E/AndroidRuntime(29538): FATAL EXCEPTION: main
02-09 22:56:10.811: E/AndroidRuntime(29538): Process: stacy.example.assignment3_stacy_v1, PID: 29538
02-09 22:56:10.811: E/AndroidRuntime(29538): java.lang.IllegalStateException: Could not execute method of the activity
02-09 22:56:10.811: E/AndroidRuntime(29538):    at android.view.View$1.onClick(View.java:3823)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at android.view.View.performClick(View.java:4438)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at android.view.View$PerformClick.run(View.java:18422)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at android.os.Handler.handleCallback(Handler.java:733)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at android.os.Looper.loop(Looper.java:136)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at android.app.ActivityThread.main(ActivityThread.java:5017)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at java.lang.reflect.Method.invokeNative(Native Method)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at java.lang.reflect.Method.invoke(Method.java:515)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at dalvik.system.NativeStart.main(Native Method)
02-09 22:56:10.811: E/AndroidRuntime(29538): Caused by: java.lang.reflect.InvocationTargetException
02-09 22:56:10.811: E/AndroidRuntime(29538):    at java.lang.reflect.Method.invokeNative(Native Method)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at java.lang.reflect.Method.invoke(Method.java:515)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at android.view.View$1.onClick(View.java:3818)
02-09 22:56:10.811: E/AndroidRuntime(29538):    ... 11 more
02-09 22:56:10.811: E/AndroidRuntime(29538): Caused by: java.lang.NumberFormatException: Invalid int: ""
02-09 22:56:10.811: E/AndroidRuntime(29538):    at java.lang.Integer.invalidInt(Integer.java:137)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at java.lang.Integer.parseInt(Integer.java:358)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at java.lang.Integer.parseInt(Integer.java:331)
02-09 22:56:10.811: E/AndroidRuntime(29538):    at stacy.example.assignment3_stacy_v1.Assignment3MainActivity.startRhythmandAnimation(Assignment3MainActivity.java:41)
02-09 22:56:10.811: E/AndroidRuntime(29538):    ... 14 more

MainActivity.java

package stacy.example.assignment3_stacy_v1;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;

public class Assignment3MainActivity extends Activity {


    private EditText mMileTimeGoal;
    private Handler mHandler;
    private View mLeftfoot;
    private Animation mFootAnim;
    private long mInterval = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assignment3_main);
        //mMileTimeGoal = findViewById(R.id.miletimegoal);
        mMileTimeGoal = (EditText) findViewById(R.id.miletimegoal);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.assignment3_main, menu);
        return true;
    }

    public void startRhythmandAnimation (View view) {

        //String MileTime = mMileTimeGoal.getContext().toString();
        String MileTime = mMileTimeGoal.getText().toString();
        String[] time_array = MileTime.split(":");
        int hours = Integer.parseInt(time_array[0]);
        int minutes = Integer.parseInt(time_array[1]);
        int seconds = Integer.parseInt(time_array[2]);
        int duration = 3600 * hours + 60 * minutes + seconds;
        int steps_per_second = 3;

        int running_rate = duration * steps_per_second;

        /*
        View rightfoot = findViewById(R.id.rightfoot);
        View leftfoot = findViewById(R.id.leftfoot);

        rightfoot.setVisibility(View.VISIBLE);
        Animation anim = AnimationUtils.makeInChildBottomAnimation(this);
        rightfoot.startAnimation(anim);

        leftfoot.setVisibility(View.VISIBLE);
        leftfoot.startAnimation(anim);
        */

        mHandler = new Handler(); //.os package class when importing
           mLeftfoot = findViewById(R.id.leftfoot);
           //mFootAnim = AnimationUtils.loadAnimation(this, R.anim.mleftfoot);
           mFootAnim = AnimationUtils.loadAnimation(this, R.id.leftfoot);
           stepRecursive();
        }

        private void stepRecursive() {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mLeftfoot.startAnimation(mFootAnim);
                    stepRecursive();
                }
            }, mInterval);
    }

    public void resetTimetoZeroes () {
        String MileTime = mMileTimeGoal.getContext().toString();
        //Int MileTime = 0;
    }

}

布局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: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=".Assignment3MainActivity" >

    <ImageView
        android:id="@+id/leftfoot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/rightfoot"
        android:layout_toLeftOf="@+id/rightfoot"
        android:src="@drawable/leftfoot" />

    <EditText
        android:id="@+id/miletimegoal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:ems="10"
        android:inputType="time"
        android:hint="Mile Time Goal?" />

    <ImageView
        android:id="@+id/rightfoot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="74dp"
        android:layout_marginRight="36dp"
        android:src="@drawable/rightfoot" />

    <Button
        android:id="@+id/startbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/leftfoot"
        android:layout_alignRight="@+id/leftfoot"
        android:onClick="startRhythmandAnimation"
        android:text="@string/start_button" />

    <Button
        android:id="@+id/resetbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/startbutton"
        android:layout_alignBottom="@+id/startbutton"
        android:layout_alignLeft="@+id/rightfoot"
        android:text="@string/reset_button"
        android:onClick="resetTimetoZeroes" />

</RelativeLayout>

编辑 - 这是最新的logcat消息。请忽略时间计算代码。我想让动画首先工作!

02-09 23:42:32.291: E/AndroidRuntime(32320): FATAL EXCEPTION: main
02-09 23:42:32.291: E/AndroidRuntime(32320): Process: stacy.example.assignment3_stacy_v1, PID: 32320
02-09 23:42:32.291: E/AndroidRuntime(32320): java.lang.IllegalStateException: Could not execute method of the activity
02-09 23:42:32.291: E/AndroidRuntime(32320):    at android.view.View$1.onClick(View.java:3823)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at android.view.View.performClick(View.java:4438)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at android.view.View$PerformClick.run(View.java:18422)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at android.os.Handler.handleCallback(Handler.java:733)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at android.os.Looper.loop(Looper.java:136)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at android.app.ActivityThread.main(ActivityThread.java:5017)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at java.lang.reflect.Method.invokeNative(Native Method)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at java.lang.reflect.Method.invoke(Method.java:515)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at dalvik.system.NativeStart.main(Native Method)
02-09 23:42:32.291: E/AndroidRuntime(32320): Caused by: java.lang.reflect.InvocationTargetException
02-09 23:42:32.291: E/AndroidRuntime(32320):    at java.lang.reflect.Method.invokeNative(Native Method)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at java.lang.reflect.Method.invoke(Method.java:515)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at android.view.View$1.onClick(View.java:3818)
02-09 23:42:32.291: E/AndroidRuntime(32320):    ... 11 more
02-09 23:42:32.291: E/AndroidRuntime(32320): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f090000 type #0x12 is not valid
02-09 23:42:32.291: E/AndroidRuntime(32320):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2314)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at android.content.res.Resources.getAnimation(Resources.java:963)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:71)
02-09 23:42:32.291: E/AndroidRuntime(32320):    at stacy.example.assignment3_stacy_v1.Assignment3MainActivity.startRhythmandAnimation(Assignment3MainActivity.java:79)

我忘记发布我的foot.xml文件了!

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="0" android:toYDelta="-15" android:duration="400"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0" android:duration="400" />
</set>

使用以下内容修复了我的资源异常:

mFootAnim = AnimationUtils.loadAnimation(this, R.anim.foot);

3 个答案:

答案 0 :(得分:2)

您的代码应如下所示,以防止异常

public void startRhythmandAnimation(View view) {

    // String MileTime = mMileTimeGoal.getContext().toString();
    String MileTime = mMileTimeGoal.getText().toString();

    DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
    try {
        Date dt = formatter.parse(MileTime);
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int hours = cal.get(Calendar.HOUR);
        int minutes = cal.get(Calendar.MINUTE);
        int seconds = cal.get(Calendar.SECOND);

        int duration = 3600 * hours + 60 * minutes + seconds;
        int steps_per_second = 3;

        int running_rate = duration * steps_per_second;

        mHandler = new Handler(); // .os package class when importing
        mLeftfoot = findViewById(R.id.leftfoot);
        // mFootAnim = AnimationUtils.loadAnimation(this, R.anim.mleftfoot);
        mFootAnim = AnimationUtils.loadAnimation(this, R.id.leftfoot);
        stepRecursive();
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(Assignment3MainActivity.this,
                "Please Enter Valid Time Stamp", Toast.LENGTH_LONG).show();
    }

}

答案 1 :(得分:1)

看来当你拆分MileTime时,数组中的一个值是一个空字符串,由以下行判断:

Caused by: java.lang.NumberFormatException: Invalid int: ""

执行拆分后放置日志语句或断点并检查time_array的值以确保

答案 2 :(得分:-1)

表示第二个错误,资源$ NotFoundException:资源ID#0x7f090000类型#0x12无效 清理项目并查看是否再次生成R.java,如果没有,请查看项目文件夹并查看是否所有文件都在那里