2013-07-30 26 views
1

**我想單擊我的片段2中的按鈕,並替換片段1與片段3,如代碼所示。
但findViewById(r.id.mybutton)正在迴歸'null'。
我試着調試代碼,但它不能夠使用所產生的「buttontoggle」
希望你能幫助我的上述問題提前:)我不知道什麼是錯誤的onClickListener按鈕在這裏片段

package com.vivekmishra1991.testfrag; 

    import android.app.Fragment; 
    import android.os.Bundle; 
    import android.support.v4.app.FragmentActivity; 
    import android.view.Menu; 
    import android.view.View; 
    import android.view.View; 
    import android.widget.Button; 


    public class MainActivity extends FragmentActivity { 

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

    if(findViewById(R.id.fragment_container)!=null){ 
     if(savedInstanceState!=null){ 
      return; 
     } 

       //fragment 1 

     f1 F1= new f1(); 
     F1.setArguments(getIntent().getExtras()); 

     getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, F1).commit(); 
        //fragment2(the fragment with the button) 

     f2 F2=new f2(); 
     F2.setArguments(getIntent().getExtras()); 
     getSupportFragmentManager().beginTransaction().add(R.id.fragment_container1, F2).addToBackStack(null).commit(); 
    } 



    //code for buttton onclick function 
    Button toggleButton = (Button)findViewById(R.id.mybutton); 

    toggleButton.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     { // fragment 3 that has to be replaced with fragment 1 
      f3 F3=new f3(); 
      F3.setArguments(getIntent().getExtras()); 
      getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,F3).addToBackStack(null).commit(); 
     } 
    }); 

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

編輯 感謝

這裏是我的activityMain.xml

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

<FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_height="0dp" 
     android:layout_weight="6" 
     android:layout_width="match_parent" /> 

<FrameLayout 
     android:id="@+id/fragment_container1" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_width="match_parent"/> 
</LinearLayout> 

f2.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"> 


<Button 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="Next!" 
     android:id="@+id/mybutton" 
     android:layout_gravity="left|center_vertical"/> 
</LinearLayout> 
+0

你有一個在activity_main.xml中定義的id爲「mybutton」的Button? –

+0

發佈您的'activity_main.xml'。 – Raghunandan

+0

您的語法沒有任何問題。似乎這是你的XML的問題。 –

回答

0

您可以檢查您的.xml文件以確保「mybutton」確實存在。

如果是這樣,請嘗試項目>清潔。偶爾,eclipse不會將該按鈕添加到R.java文件中,並且清理該項目往往會修復該問題。如果它不是這樣,你總是可以嘗試在你的xml文件中重新創建按鈕。

相關問題