2016-04-01 23 views
0

Blockquoteint android.widget.TextView.getVisibility()'

Ben Fragment içeride TextView yanıp istiyorum ama bunun bir hata referans boş nesnesi benim kodudur veren oldu. Gerçekten burada çok

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

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 takdir herhangi biri yardımcı olabilir lütfen android için yeni

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

+0

Tüm hata iletisini gönderir misiniz? –

+1

R.layout.fragment_one dosyasında id (@ + id/use) içeren bir TextView içeriyor muydunuz? –

+0

yp bu pencereden bir metin görünümü var –

cevap

1

İşi değiştiriyorum, düzeninizi ihtiyacınıza göre değiştirin. Eğer içinde herhangi EDITTEXT veya autocompleteTextview olmadan textinputlayout varsa 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(); 
} 

Benim 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> 
+0

txt.getVisibility() öğesinin, txt yerine gerekli booleanın int –

+0

olduğunu gösteren bir hata olarak altı çizildi. getVisibility() koymak txt.isShown() –

+0

java.lang.NullPointerException: sanal yöntem 'boolean android.widget.TextView.isShown()' null bir nesne başvuru –

0

, boş işaretçi özel durum oluşturur daha.

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

//Put some textview here 

</android.support.design.widget.TextInputLayout> 
İlgili konular