0

I am making a fade-in animation to a textview in my project but the application crushes with a null pointer exception

the target is the last textview with id(long_click) and here is the xml file

    <?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>

the animation xml is

<?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>

the function which calls the animation is

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();
    }

the animation file is located in R.anim.fade_in

the error is the following

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'

I tried to change the target of the animation to another view in the same activity and yet getting the same result

after solving the problem the animation is not working as like there is no animation at all i updated the animation file to

<?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>

but still the animation is not working help me please

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
Ahmed Rajab
  • 429
  • 2
  • 8
  • 24

1 Answers1

1

you forgot to call setContentView(R.layout.yourXMLcontainsTextView); before text_fade_in(); function call

@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();
}
Pavneet_Singh
  • 34,557
  • 5
  • 43
  • 59