Java setVisibility(View.GONE)不工作!为什么?

Java setVisibility(View.GONE)不工作!为什么?,java,android,xml,Java,Android,Xml,我可以向任何你喜欢的东西发誓,这段代码是在我以前的应用程序上工作的,我当时正在工作,但现在它什么也做不了,这应该是一件简单的事情,我必须在使用这段代码之前忘记,但我不知道它是什么 我试图完成的是使按钮消失,并使布局可见 以下是我的xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.andro

我可以向任何你喜欢的东西发誓,这段代码是在我以前的应用程序上工作的,我当时正在工作,但现在它什么也做不了,这应该是一件简单的事情,我必须在使用这段代码之前忘记,但我不知道它是什么

我试图完成的是使按钮消失,并使布局可见

以下是我的xml:

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

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#FFFFFF"
        android:layout_marginBottom="2dp">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1.0">

            <ImageView
                android:src="@drawable/natlilandbarpic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>

        <Button
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:background="@drawable/drawbtn"
            android:id="@+id/drawbutton"
            android:visibility="visible"/>

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="right"
        android:layout_weight="1.0">

        <WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Text"
            android:id="@+id/webView"/>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            android:gravity="left"
            android:layout_marginLeft="100dp"
            android:id="@+id/drawlist">

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button"/>

        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

如果您能提供任何帮助和技巧,我将不胜感激。

首先,我想您之所以会遇到这种情况,是因为您没有调用函数addListenerOnButton(),因此必须在oncreate中调用它,其次,您正在调用函数,该函数在单击按钮后也会将drawlist可见性设置为visible,但您必须知道,您在单击按钮后试图查找drawlist的引用,这将导致应用程序崩溃,因此您需要将这行代码“drawlist=(LinearLayout)findViewById”(R.id.drawlist);“除了第一次找到按钮的viewById外,希望这对您有所帮助。

您永远不会调用您的
addListenerOnButton()
方法,因此从未分配
drawbutton
drawlist
,并且从未设置
OnClickListener
。因此drawbuttonprop不起作用?那么分配它们的正确方法是什么?您调用
drawbuttonprop()的唯一位置是在
addListenerOnButton()中设置的
OnClickListener
。不过,您当前没有调用
addListenerOnButton()
,因此这一点从未设置。我不确定“正确的方法”是什么意思,但如果您只是在
setContentView()之后从
onCreate()
调用您的
addListenerOnButton()
,那么你所拥有的应该可以工作,假设这是正确的,当前的布局。哦,天哪,我知道我忘记了smthing tnx,它让我发疯了唯一的问题是调用函数“addListenerOnButton();”tnx寻求帮助,虽然我很乐意帮助mate,祝你编码好运
    public class MainActivity extends Activity 
    {
        private WebView webView;
        private Button drawbutton;
        private LinearLayout drawlist;
        
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            getActionBar().hide();
            
            
            webView = (WebView) findViewById(R.id.webView);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl("");
            webView.setWebViewClient(new WebViewClient(){
    
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url){
                        view.loadUrl(url);
                        return true;
                    }
                });
            }
                
        public void addListenerOnButton() {
            
            drawbutton = (Button) findViewById(R.id.drawbutton);
            drawbutton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View p) {   
                    drawbuttonprop(p);
                    }
                });
            drawlist = (LinearLayout) findViewById(R.id.drawlist);
        }
        public void drawbuttonprop(View view)
        {
            drawbutton.setVisibility(View.GONE);
            drawlist.setVisibility(View.VISIBLE);
        }

}