int android.widget.TextView.getVisibility()'在null对象引用上

时间:2016-04-01 16:42:48

标签: android android-fragments

  

块引用

我想在TextView内眨眼Fragment但是这是一个错误引用null对象这里是我的代码。我是android新手,任何人都可以帮助我非常感激

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

这是错误

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"/>

2 个答案:

答案 0 :(得分:1)

我正在进行切换,根据您的需要更改布局。 我的片段

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

我的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>

答案 1 :(得分:0)

如果你的textinputlayout里面没有任何edittext或autocompleteTextview,那么它会抛出空指针异常。

<android.support.design.widget.TextInputLayout
                                            android:layout_width="match_parent"
                                            android:layout_height="wrap_content">

//Put some textview here

</android.support.design.widget.TextInputLayout>