I'm taking a Coursera Android course and am trying to complete an app that I started. I'm trying to get an animation to occur at set intervals. See here for more details
I looked at a lot of posts with the same title, but everyone seems to have different solutions. I don't get an errors in Eclipse when it saves and compiles.
Here's my Logcat results:
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;
}
}
Layout 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>
EDIT - Here's the latest logcat messages. Please ignore the time calculation code. I would like to get the animation working first!
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)
I forgot to post my foot.xml file!
<?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>
Fixed my Resource Exception with the following:
mFootAnim = AnimationUtils.loadAnimation(this, R.anim.foot);
for the 2nd error,Resources$NotFoundException: Resource ID #0x7f090000 type #0x12 is not valid clean the project and see if R.java gets generated again, if not, look at your project folder and see if all files are there
It seems that when you split MileTime, one of the values in the array is an empty string, judging by the following line:
Put a log statement or breakpoint after you do the split and check the values of
time_array
to be sureYour code should be like below to prevent exception