2013-12-19 36 views
0

我是新來的android編程和一些奇怪的事情發生在我的應用程序後,我在我的佈局中移動按鈕後。 所以有一個按鈕(開始),當點擊時,啓動另一個活動。這是代碼。Button2正在做按鈕1應該做的工作

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button startbutton = (Button) findViewById(R.id.button1); 

     startbutton.setOnClickListener(new View.OnClickListener() {  //STARTBUTTON 
      @Override 
      public void onClick(View arg0) {   //LO QUE HACE EL START BUTTON 
       Intent intent = new Intent(MainActivity.this, JugarActivity.class); 
       startActivity(intent); 
      } 
     }); 
    } 
} 

我的問題是測試我的應用程序時,當我點擊開始按鈕沒有任何反應,但是當我點擊另一個按鈕我在我的佈局(將Button4)它給我的第二個活動。 這是我的activity_main.xml以防萬一需要。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center_horizontal" 
android:orientation="vertical" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="150dp" 
    android:minWidth="200dp" 
    android:text="@string/Start" 
    android:textSize="30sp" /> 

<Button 
    android:id="@+id/button4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:maxWidth="200dp" 
    android:minHeight="50dp" 
    android:minWidth="200dp" 
    android:text="@string/Jugarporcontinente" 
    android:textSize="30sp" /> 

<Button 
    android:id="@+id/button5" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:minWidth="200dp" 
    android:text="@string/Aprende" 
    android:textSize="30sp" /> 

<Button 
    android:id="@+id/button6" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:minWidth="200dp" 
    android:text="@string/Puntajes" 
    android:textSize="30sp" /> 

<Button 
    android:id="@+id/button7" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:minWidth="200dp" 
    android:text="@string/Opciones" 
    android:textSize="30sp" /> 

</LinearLayout> 

我不明白如何點擊一個甚至沒有代碼的按鈕,從另一個按鈕啓動代碼。歡迎任何幫助。謝謝。

+0

我剛剛編輯你的問題,添加一些更好的縮進,實際上匹配大括號。爲了保持你的代碼可讀 - 對我們和你來說 - 保持匹配的大括號,並打算記住。 – cincodenada

+0

提供的代碼是按鈕1? – iWumbo

+1

@iWumbo你的問題沒有任何意義 - 這段代碼在'MainActivity :: onCreate'中,'startbutton'被'findViewById(R.id.button1)'引用,所以是的。 – cincodenada

回答

1

基於問題的評論,在這裏發生了什麼事:

當資源被編譯,資源XML文件被編譯爲二進制XML格式和資源標識符R.java產生。生成的標識符指向資源二進制文件中的特定項目。標識符只是一個序列中的整數,值按照它們在資源XML文件中聲明的順序給出。

大多數情況下,這項工作都可以。修改源XML文件時,這些更改將編譯爲二進制資源和R類。

但是,有時構建依賴系統無法正常工作。例如,R中的標識符已更新,但並非所有二進制資源都反映已更改的標識符。當你嘗試在代碼中通過id引用一個元素時,它實際上會返回一些在二進制資源中用該id指定的其他元素。

在這種情況下,你是「幸運」 - 有通過在層次結構ID的視圖(沒有空回,沒有NullPointerException),它正好是一個Button(無ClassCastException)。

清理項目會同時刪除編譯的資源二進制文件和生成的R.java,所以在重新編譯時它們會再次同步。

相關問題