我正在为项目中的textview制作一个淡入动画 但应用程序使用空指针异常
进行压缩目标是最后一个带id的文本视图(long_click) 这是xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_more_options_passowrd_groups"
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="arb.passwordmanager.more_options_passowrd_groups"
android:background="@drawable/settings"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:background="#88FFFFFF"
android:id="@+id/groups_layout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="@string/char_group"
android:textColor="#222222"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/char_group_explain"
android:textColor="#444444"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:id="@+id/checkboxes"
android:orientation="vertical"
></LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FF4444"></View>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/check_uncheck_all"
android:id="@+id/checkall"
android:checked="true"
android:textColor="#444444"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/long_click_item"
android:textColor="#444444"
android:id="@+id/long_click"/>
</LinearLayout>
</RelativeLayout>
动画xml是
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" >
<alpha android:fromAlpha="0" android:toAlpha="1" />
</set>
调用动画的函数是
void text_fade_in()
{
TextView textView=(TextView) findViewById(R.id.long_click);
Animation animation= AnimationUtils.loadAnimation(getBaseContext(),R.anim.fade_in);
textView.startAnimation(animation);//the exception is here
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_more_options_passowrd_groups);
text_fade_in();
}
动画文件位于R.anim.fade_in
错误如下
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{arb.passwordmanager/arb.passwordmanager.more_options_passowrd_groups}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
at android.app.ActivityThread.access$700(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
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:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at arb.passwordmanager.more_options_passowrd_groups.text_fade_in(more_options_passowrd_groups.java:170)
at arb.passwordmanager.more_options_passowrd_groups.onCreate(more_options_passowrd_groups.java:176)
at android.app.Activity.performCreate(Activity.java:5326)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
at android.app.ActivityThread.access$700(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
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:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
I/Process: Sending signal. PID: 30468 SIG: 9
Disconnected from the target VM, address: 'localhost:8618', transport: 'socket'
我尝试将动画的目标更改为同一活动中的另一个视图但获得相同的结果
解决问题后,动画无法正常工作,根本没有动画 我将动画文件更新为
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:fillBefore="true" android:interpolator="@android:anim/accelerate_interpolator" >
<alpha android:fromAlpha="0" android:toAlpha="1" android:duration="3000" />
</set>
但动画仍无效 请帮帮我
答案 0 :(得分:1)
您忘记在setContentView(R.layout.yourXMLcontainsTextView);
函数调用
text_fade_in();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourXMLcontainsTextView); // add this , otherwise there is no design
// linked with your activity and cannot be found using findViewById
text_fade_in();
}