1

Coursera Android 코스를 수강 중이며 시작한 앱을 완성하려고합니다. 나는 설정된 간격으로 애니메이션을 얻으려고 노력하고있다. See here for more details안드로이드 - IllegalStateException : InvocationTargetException에 의해 발생 된 활동의 메소드를 실행할 수 없습니다.

같은 제목의 게시물을 많이 보았지만 모두 다른 해결책이있는 것으로 보입니다. Eclipse에서 저장하고 컴파일 할 때 오류가 발생하지 않습니다.

여기에 내 로그 캣 결과입니다 :

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> 

편집 - 여기에 최신 로그 캣 메시지입니다. 시간 계산 코드를 무시하십시오. 애니메이션 작업을 먼저하고 싶습니다!

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

귀하의 코드는 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

나는이 라인을 지나서는 얻을 수 없다 : 날짜 dt = formatter. 구문 분석 (MileTime); 처리되지 않은 예외 유형은 ParseException입니다. – StacyM

+0

@StacyM, 인수에 "18:12:02"을 전달하여 코드를 검사했습니다. 잘 작동합니다. 당신은 형식으로 가치를 전달하지 않습니다. :) –

+0

아직 02-10 00 : 13 : 23.651 : E/AndroidRuntime (3688) : 원인 : java.text.ParseException : Unparseable date : "104040"(오프셋 6) 토스트 메시지 대신! Unparseable 날짜 오류로 인해 응용 프로그램이 충돌하는 것을 방지하는 캐치가 왜 아닌가요? – StacyM

1

당신이 배열에 MileTime, 값 중 하나를 분할 할 때 다음 줄에 의해 판단, 빈 문자열 것 같다 :

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

분할을 수행 한 후 로그 문이나 중단 점을 넣고값을 확인하십시오.이

+0

좋아, 그게 도움이. NumberFormat Exception을 수정해야 할 일반적인 아이디어가 있습니다. 나는 아직도 이것에 대해 무엇을 해야할지 모르겠다 - 원인 : java.lang.reflect.InvocationTargetException – StacyM

+0

근본 원인을 찾기 위해 전체 스택 추적이 필요하다. –

-1

을 방지하기 위해 아래와 같이해야한다 확인하기 위해, 자원 $ NotFoundException : 자원 ID # 0x7f090000 형 # 0x12로는 깨끗한 프로젝트와 있는지 유효하지 않습니다 R.java가 다시 생성되면, 그렇지 않다면, 프로젝트 폴더를보고 모든 파일이 있는지 확인하십시오.

관련 문제