我有一个快餐栏,我想要删除textview,但是当我使用View.INVISIBLE时它没有显示uo但是当使用View.GONE时,app会因为说NullPointerException而崩溃。为什么这样?
这是代码
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()'在null对象引用上 在android.support.design.internal.SnackbarContentLayout.onMeasure(SnackbarContentLayout.java:85) 在android.view.View.measure(View.java:18820) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5961) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在android.support.design.widget.Snackbar $ SnackbarLayout.onMeasure(Snackbar.java:335) 在android.view.View.measure(View.java:18820) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5961) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) 在android.view.View.measure(View.java:18820) 在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) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 在android.view.View.measure(View.java:18820) 在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.run(Choreographer.java:858) 在android.view.Choreographer.doCallbacks(Choreographer.java:670) 在android.view.Choreographer.doFrame(Choreographer.java:606) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5437) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
首先尝试创建像这样的Snackbar
// Create the Snackbar
Snackbar snackbar = Snackbar.make(containerLayout, "", Snackbar.LENGTH_LONG);
获取Snackbar的布局视图
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);
将视图添加到Snackbar的布局
layout.addView(snackView, 0);
显示Snackbar
snackbar.show();