Siluni Upeksha Siluni Upeksha - 3 months ago 13
Android Question

int android.widget.TextView.getVisibility()' on a null object reference


Blockquote


I want to blink
TextView
inside
Fragment
but this was giving an error reference null object here is my code. I am new to android please any one can help really appreciate it very much

public class TwoFragment extends Fragment {
View inflaterView;

public TwoFragment() {
// Required empty public constructor



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflaterView = inflater.inflate(R.layout.fragment_one, container, false);

blink();
// Inflate the layout for this fragment
return inflaterView;
}


private void blink() {
final Handler handler = new Handler();

new Thread(new Runnable() {
@Override
public void run() {
int timeToBlink = 1000;
//in milissegunds
try {
Thread.sleep(timeToBlink);

} catch (Exception e) {

}

handler.post(new Runnable() {
@Override
public void run() {
TextView txt = (TextView) inflaterView.findViewById(R.id.usage);

if (txt.getVisibility() == inflaterView.VISIBLE) {
txt.setVisibility(inflaterView.INVISIBLE);

} else {
txt.setVisibility(inflaterView.VISIBLE);

}

blink();
}
});
}
}).start();
}
}



here is the error


04-01 22:25:34.471 27846-27846/com.example.siluni.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.siluni.myapplication, PID: 27846
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.TextView.getVisibility()' on a null object reference
at com.example.siluni.myapplication.TwoFragment$1$1.run(TwoFragment.java:59)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5930)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)



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"
tools:context="info.androidhive.materialtabs.fragments.OneFragment">

<TextView
android:id="@+id/usage"
android:layout_marginTop="220dip"
android:layout_marginLeft="45dip"
android:layout_marginRight="15dip"
android:typeface="serif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Google "
android:textColor="#030900"/>



Answer

I am getting the toggling ,change your layout according to your need. My Fragment

public class MyFragment extends Fragment {
TextView txt;
View inflaterView;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    inflaterView = inflater.inflate(R.layout.de, container, false);
    txt= (TextView) inflaterView.findViewById(R.id.type);
    blink();
    // Inflate the layout for this fragment
    return inflaterView;
}


private void blink() {
    final Handler handler = new Handler();

    new Thread(new Runnable() {
        @Override
        public void run() {
            int timeToBlink = 1000;
            //in milissegunds
            try {
                Thread.sleep(timeToBlink);

            } catch (Exception e) {

            }

            handler.post(new Runnable() {
                @Override
                public void run() {


                    if (txt.isShown() ) {
                        txt.setVisibility(View.INVISIBLE);

                    } else {
                        txt.setVisibility(View.VISIBLE);

                    }

                    blink();
                }
            });
        }
    }).start();
}

My Xml

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

    <TextView
        android:id="@+id/type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight=".5"
        android:text="jekjkejd"
        android:textStyle="bold"

        android:textColor="#000000"
        android:layout_marginRight="40dp"

        />


</LinearLayout>