2015-11-01 112 views
0

好吧,我终于弄清楚了我的问题的根源。一系列测试后,并尝试不同的事情,我注意到,我把onCreateView代码button.setEnabled上的Android崩溃(false)

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

后,它会在启动时崩溃。这是在我的menu1_Fragment.java,是我的第一个片段。在我的课上,我首先定义了我的按钮。

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

然后我在onCreateView做的第一件事就是运行功能SET按钮();

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

这里会发生什么情况是,这些按钮实际上是在我menu2_layout.xml这是我的第二个片段,但我rootview让我找到它。然后,我在下面定义了我的按钮之后,将我的所有按钮设置为false,如第一位代码所示,因为我不希望用户单击按钮。这是我发现它在开始时崩溃的地方。这是我的按钮的代码。

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

我给我的所有按钮自己selector_state,因为这是最初我以为这个问题是。这是我的选择器的代码。

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

如果你能帮帮我一直停留在这个问题上的很长一段时间,并且更加的帖子,然后我需要,但我似乎无法来解决这个问题。此外,我无法访问我的日志,因为我有技术问题,基本上冻结我的android工作室,当我尝试点击Android监视器,我的应用程序正在运行。

如果这一切还不够我也贴在GitHub上

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

我会尽量解决尽可能多的问题,你可能有,但我新的,这是我的第一次应用。我甚至没有这么做过一个星期,所以请耐心等待。

回答

1

这里会发生什么情况是,这些按钮实际上是在我 menu2_layout.xml这是我的第二个片段,但我rootview让我 找到它。

我通常会说,发布错误,但这个已抛出我,

rootview是menu1_layou.xml,按钮都在menu2_layout.xml。 因此,您需要在包含您正在查找的ID的View上调用findViewById()。

是的代码将编译和您的IDE不会抛出任何错误,但这并不意味着它会工作。

如果解决此问题后仍然有问题,请发布崩溃日志。

+0

我的rootview实际上是在MyActivity上创建的。java,但我只设置rootview =我的menu1_layout在我应该实际改变的开始 – TeenCoder

+0

rootview = inflater.inflate(R.layout.menu1_layout,container,false); – BrendanDodd

+0

有没有办法将rootview设置为1以上?编辑*有一种方法可以返回超过1个rootview。 – TeenCoder