首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android - IllegalStateException:无法执行由InvocationTargetException引起的活动的方法。

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

Stack Overflow用户
提问于 2014-02-10 07:17:16
回答 3查看 6.5K关注 0票数 1

我正在选修Coursera课程,并试图完成我刚开始的一个应用程序。我试着让动画在一定的时间间隔内发生。See here for more details

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

这是我的日志结果:

代码语言:javascript
运行
复制
    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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
<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消息。请忽略时间计算代码。我想让动画先开始工作!

代码语言:javascript
运行
复制
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文件!

代码语言:javascript
运行
复制
<?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>

用以下方法修正了我的资源异常:

代码语言:javascript
运行
复制
mFootAnim = AnimationUtils.loadAnimation(this, R.anim.foot);
EN

Stack Overflow用户

发布于 2014-02-10 07:53:58

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

票数 -1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21671129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档