1

我正在参加Coursera Android课程,正在尝试完成我开始的一个应用程序。我试图让动画以设定的时间间隔出现。 See here for more detailsAndroid - IllegalStateException:无法执行活动的方法,由InvocationTargetException造成

我看了很多相同标题的帖子,但每个人似乎都有不同的解决方案。在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); 
+0

这是您的行号41? –

+0

第41行是int hours = Integer.parseInt(time_array [0]); – StacyM

+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(); 
    } 

} 
+0

我无法越过这条线:Date dt = formatter。解析(MileTime);未处理的异常类型ParseException。 – StacyM

+0

@StacyM,我通过在参数中传递“18:12:02”来检查我的代码。它的工作正常。你不是格式化的价值。 :) –

+0

我仍然得到02-10 00:13:23.651:E/AndroidRuntime(3688):导致:java.text.ParseException:Unparseable日期:“104040”(在偏移量6) 而不是您的Toast消息!为什么不能防止应用程序崩溃,导致Unparseable日期错误? – StacyM

1

看来,当你在阵列中分裂MileTime,其中一个值是一个空字符串,通过以下行判断:

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

在执行拆分后检查日志语句或断点,并检查time_array,以确保

+0

好的,这有帮助。对于NumberFormat异常需要解决的问题我有一个大概的想法。我仍然不知道该怎么做这个 - 引发:java.lang.reflect.InvocationTargetException – StacyM

+0

我需要一个完整的堆栈跟踪来找到根本原因 –

-1

的第2次故障,资源$ NotFoundException:资源ID#0x7f090000型#0×12是无效 干净的项目,看看是否R.java会再次生成,如果没有,请查看您的项目文件夹并查看是否有所有文件

相关问题