简体   繁体   English

Android在button.setEnabled(false)上崩溃

[英]Android crash on button.setEnabled(false)

Ok so I finally figured out the source of my problem. 好的,所以我终于找到了问题的根源。 After a bunch of tests and trying out different things I noticed that after I put the codes 经过一堆测试并尝试了不同的方法后,我注意到在放置代码后

button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);

in onCreateView, it would crash upon startup. 在onCreateView中,它将在启动时崩溃。 This is in my menu1_Fragment.java and is my first fragment. 这是在我的menu1_Fragment.java中,这是我的第一个片段。 In my class I start off by defining my buttons. 在课堂上,我先定义按钮。

Button button1;
Button button2;
Button button3;
Button button4;

Then the first thing I do in onCreateView is run the function setButton(); 然后,我在onCreateView中要做的第一件事就是运行setButton()函数;

    public void setButton() {
            button1 = (Button) rootview.findViewById(R.id.upgbutton);
            button2 = (Button) rootview.findViewById(R.id.upgbutton1);
            button3 = (Button) rootview.findViewById(R.id.upgbutton2);
            button4 = (Button) rootview.findViewById(R.id.upgbutton3);
        }

What happens here is that these buttons are actually in my menu2_layout.xml which is my second fragment but my rootview allows me to find it. 这里发生的是这些按钮实际上在我的menu2_layout.xml中,这是我的第二个片段,但是我的rootview允许我找到它。 Then after I defined my buttons right below I set all my buttons to false as seen in the first bit of code because I don't want the user to be clicking the buttons yet. 然后,在右下方定义按钮之后,将所有按钮设置为false(如第一部分代码所示),因为我不希望用户单击按钮。 This is where I found that it crashes upon start. 这是我发现启动时崩溃的地方。 Here is the code for my buttons. 这是我的按钮的代码。

<LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:weightSum="4">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selector_state2_xml"
            android:id="@+id/upgbutton"
            android:layout_gravity="center_horizontal" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selector_state3_xml"
            android:id="@+id/upgbutton1"
            android:layout_gravity="center_horizontal" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selector_state4_xml"
            android:id="@+id/upgbutton2"
            android:layout_gravity="center_horizontal" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/selector_state5_xml"
            android:id="@+id/upgbutton3"
            android:layout_gravity="center_horizontal" />


            </LinearLayout>

I gave all my buttons their own selector_state because that is what initially I thought the problem was. 我为所有按钮赋予了自己的选择器状态,因为起初我以为是问题所在。 Here is the code for my selector. 这是我的选择器的代码。

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/button"></item>
    <item android:drawable="@drawable/buttonclick" android:state_pressed="true"></item>
    <item android:drawable="@drawable/buttongray" android:state_enabled="false"></item>

</selector>

If you can please help I have been stuck on this issue for a really long time and made more posts then I need to, but I just can't seem to solve it. 如果可以的话,我已经在这个问题上停留了很长时间,并且发布了更多文章,但我确实需要解决,但是我似乎无法解决。 Also I cannot access my logs as I am having technical issues which basically freeze my android studio when I try to click android monitor and my app is running. 另外,由于遇到技术问题,当我尝试单击android Monitor且我的应用正在运行时,我的android studio基本上冻结了,因此我无法访问日志。

If all this isn't enough I also posted on github 如果这还不够,我也发布在github上

https://github.com/BeniReydman/Slide_Menu-Slide_Menu https://github.com/BeniReydman/Slide_Menu-Slide_Menu

I will try to solve as much questions as you may have, but I am new and this is my first App. 我将尝试解决您可能遇到的所有问题,但这是我的新手,这是我的第一个应用程序。 I haven't even been doing this for over a week so please bear with it. 我什至没有这样做一个星期,所以请忍受。

What happens here is that these buttons are actually in my menu2_layout.xml which is my second fragment but my rootview allows me to find it. 这里发生的是这些按钮实际上在我的menu2_layout.xml中,这是我的第二个片段,但是我的rootview允许我找到它。

I would usually say post the error but this has thrown me, 我通常会说发布错误,但是这使我失望了,

rootview is menu1_layou.xml, the buttons are in menu2_layout.xml. rootview是menu1_layou.xml,按钮在menu2_layout.xml中。 So you need to call findViewById() on the View that contains the ID's you are looking for. 因此,您需要在包含要查找的ID的视图上调用findViewById()。

Yes the code will compile and your IDE will not throw any errors but that doesn't mean it will work. 是的,代码将编译,您的IDE将不会引发任何错误,但这并不意味着它将起作用。

If you are still having problems after you resolve this then please post the crash logs. 解决此问题后,如果仍然有问题,请发布崩溃日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM