动画上的NullPointerException

时间:2015-06-25 12:24:39

标签: java android animation nullpointerexception

我试图制作动画,但我只看到NullPointerException

logcat的:

06-25 12:13:40.474  12959-12959/com.example.admin.p0201_simpleanimation E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.example.admin.p0201_simpleanimation.MainActivity.onContextItemSelected(MainActivity.java:92)
        at android.app.Activity.onMenuItemSelected(Activity.java:2566)
        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:353)
        at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:144)
        at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:99)
        at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3633)
        at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
        at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
        at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
        at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
        at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:941)
        at android.widget.AdapterView.performItemClick(AdapterView.java:298)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
        at android.widget.AbsListView$1.run(AbsListView.java:3423)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

我只想使用上下文菜单为简单的TextView制作动画。 异常原因隐藏在这里

  

textView.startAnimation(阿尼姆);

我的onContextMenuItemSelected方法:

 @Override
public boolean onContextItemSelected(MenuItem item) {

    Animation anim = null;
    switch (item.getItemId()) {
        case MENU_ALPHA_ID:
            anim = AnimationUtils.loadAnimation(this, R.anim.myalpha);
            break;
        case MENU_COMBO_ID:
            anim = AnimationUtils.loadAnimation(this, R.anim.mycombo);
            break;
        case MENU_ROTATE_ID:
            anim = AnimationUtils.loadAnimation(this, R.anim.myrotate);
            break;
        case MENU_SCALE_ID:
            anim = AnimationUtils.loadAnimation(this, R.anim.myscale);
            break;
        case MENU_TRANSLATE_ID:
            anim = AnimationUtils.loadAnimation(this, R.anim.mytrans);
            break;
    }
        textView.startAnimation(anim);

    return super.onContextItemSelected(item);
}

我的一个XML动画文件:

<?xml version="1.0" encoding="utf-8"?>
<alpha
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="3000">
</alpha>`

此文件位于

  

RES /动画/ myalpha.xml

请帮帮我。怎么了?

4 个答案:

答案 0 :(得分:1)

你初看了textview吗?使用findViewById()

答案 1 :(得分:1)

异常隐藏原因

textView.startAnimation(anim);

此处NPE的唯一方法是textViewnull。确保已初始化它。

答案 2 :(得分:0)

为switch case语句添加默认值,如下所示,

      case MENU_TRANSLATE_ID:
       default::
          anim = AnimationUtils.loadAnimation(this, R.anim.mytrans);
          break;

如果ID与任何人都不匹配,则可能会导致NullPointerException

答案 3 :(得分:0)

尝试更改行

Animation anim = null;

Animation anim = new Animation();