2017-08-19 75 views
-2

我有,我想刪除TextView的,但是當我使用View.INVISIBLE那麼它沒有顯示UO但使用View.GONE時則應用程序是說NullPointerException異常崩潰的小吃店。爲什麼這樣?爲什麼View.Gone不是在小吃吧工作?

下面是代碼

MainActivity.java

Snackbar sk=Snackbar.make(findViewById(R.id.ll),"It is checked",Snackbar.LENGTH_INDEFINITE); 
sk.setAction("Undo",m); 
sk.setActionTextColor(Color.GREEN);  

Snackbar.SnackbarLayout sl = (Snackbar.SnackbarLayout) sk.getView(); 

TextView t=sl.findViewById(android.support.design.R.id.snackbar_text); 
t.setVisibility(View.GONE); 

logcat的

致命異常:主 工藝:com.example.ankit.testing,PID:29950 的java。 lang.NullPointerException:嘗試調用虛擬方法上一個空對象引用「INT android.text.Layout.getLineCount()」at android.support.design.internal.SnackbarContentLayout.onMeasure(SnackbarContentLayout.java:85) at android.view.View.measure(View.java:18820) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 5961) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at android.support.design.widget.Snackbar $ SnackbarLayout.onMeasure(Snackbar.java:335) at android.view.View.measure( View.java:18820) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5961) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) at android.view.View.measure(View.java:18820) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5961 ) 在android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391) 在android.view.View.measure(View.java:18820) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java :5961) at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) at android.view.View.measure(View.java:18820) at android.view.ViewGroup.measureChildWithMargins( ViewGroup.java:5961) 在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 在android.widget.LinearLayout.onMeasure(LinearLayout中。的java:630) 在android.view.View.measure(View.java:18820) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5961) 在android.widget.FrameLayout.onMeasure(FrameLayout.java: 194) 在com.android.internal.policy.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2680) 在android.view.View.measure(View.java:18820) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2132) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1248) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1484) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6091) 在android.view.Choreographer $ CallbackRecord。運行(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer $ FrameDisplayEventReceiver。運行(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop( Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5437) at java.lang.reflect.Method.invoke(Native Method) at com.android.inte rnal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

+0

https://stackoverflow.com/questions/32453946/how-to-customize-snackbars-layout/41154330 –

回答

0

先試試這個創建小吃吧狀這

// Create the Snackbar 
Snackbar snackbar = Snackbar.make(containerLayout, "", Snackbar.LENGTH_LONG); 

獲取小吃吧的佈局來看

Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView(); 

隱藏小吃吧的這樣

TextView textView = (TextView) layout.findViewById(android.support.design.R.id.snackbar_text); 
    textView.setVisibility(View.INVISIBLE); 

集文本的文本到小吃吧這樣

TextView textViewTop = (TextView)  
snackView.findViewById(R.id.text); 
textViewTop.setText(text); 

視圖添加到小吃吧的佈局

layout.addView(snackView, 0); 

秀小吃店

snackbar.show(); 
+0

感謝名單.....但我不希望添加其他laout。我只是想通過View.GONE刪除文本。爲什麼它不起作用。那是我的問題 –

相關問題