Android动画 - 不幸的是程序停止了

时间:2014-07-07 11:00:47

标签: android xml eclipse android-layout animation

我在Android上编写应用程序...我想在我的应用程序的第一页上有一个imageView&带有动画的textView但我的应用程序向我显示了这个错误:" Unfortunetly程序已经停止" .. 怎么了?我该怎么办?

的src / PlantActivity.java:

package com.AdMd.Plant;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class PlantActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent i = new Intent(PlantActivity.this, FadeInActivity.class);
        startActivity(i);
    }
}

的src / FadeInActivity.java

package com.AdMd.Plant;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FadeInActivity extends Activity implements AnimationListener {

    TextView txtMessage;
    ImageView img;

    // Animation
    Animation animFadein;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        getActionBar().hide();
        setContentView(R.layout.page1);

        txtMessage = (TextView) findViewById(R.id.textView1);
        img = (ImageView) findViewById(R.id.imageView1);

        // load the animation
        animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.fade_in);

        // set animation listener
        animFadein.setAnimationListener(this);

        // button click event
        //btnStart.setOnClickListener(new View.OnClickListener() {

            //@Override
            //public void onClick(View v) {
                txtMessage.setVisibility(View.VISIBLE);
                img.setVisibility(View.VISIBLE);
                // start the animation
                txtMessage.startAnimation(animFadein);
                img.startAnimation(animFadein);
            //}
    //  });

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        // Take any action after completing the animation

        // check for fade in animation
        if (animation == animFadein) {
            Toast.makeText(getApplicationContext(), "Animation Stopped",
                    Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

    }
}

RES /动画/ fade_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />

</set>

RES /布局/ page1.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/icon2" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="160dp"
        android:text="@string/app_info" />

</RelativeLayout>

LogCat消息是:

07-07 11:26:55.792: D/AndroidRuntime(1957): Shutting down VM
07-07 11:26:55.792: W/dalvikvm(1957): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
07-07 11:26:55.812: E/AndroidRuntime(1957): FATAL EXCEPTION: main
07-07 11:26:55.812: E/AndroidRuntime(1957): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.AdMd.Plant/com.AdMd.Plant.PlantActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.AdMd.Plant/com.AdMd.Plant.FadeInActivity}; have you declared this activity in your AndroidManifest.xml?
07-07 11:26:55.812: E/AndroidRuntime(1957):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at android.os.Looper.loop(Looper.java:137)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at java.lang.reflect.Method.invokeNative(Native Method)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at java.lang.reflect.Method.invoke(Method.java:511)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at dalvik.system.NativeStart.main(Native Method)
07-07 11:26:55.812: E/AndroidRuntime(1957): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.AdMd.Plant/com.AdMd.Plant.FadeInActivity}; have you declared this activity in your AndroidManifest.xml?
07-07 11:26:55.812: E/AndroidRuntime(1957):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at android.app.Activity.startActivityForResult(Activity.java:3190)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at android.app.Activity.startActivity(Activity.java:3297)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at com.AdMd.Plant.PlantActivity.onCreate(PlantActivity.java:14)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at android.app.Activity.performCreate(Activity.java:4465)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-07 11:26:55.812: E/AndroidRuntime(1957):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-07 11:26:55.812: E/AndroidRuntime(1957):     ... 11 more
07-07 11:27:00.962: I/Process(1957): Sending signal. PID: 1957 SIG: 9

PlantManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.AdMd.Plant"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="14" />

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".PlantActivity" >
            <activity android:name=".FadeInActivity" android:label="@string/app_name" > </activity>
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

看着这个错误:

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.AdMd.Plant/com.AdMd.Plant.FadeInActivity}; have you declared this activity in your AndroidManifest.xml?
07-07 11:06:44.762: E/AndroidRuntime(1521): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)

您是否在AndroidManifest中初始化了您的Activity FadeInActivity?

每个活动都需要在AndroidManifest中初始化才能运行(:

编辑:

您在另一个活动的嵌套中添加了活动。它应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AdMd.Plant"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="14" />

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".PlantActivity" >

        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".FadeInActivity" android:label="@string/app_name" ></activity>

</application>

</manifest>