2016-04-30 87 views
-2

我是新來的Android和掙扎與我的片段項目,請幫助我! 這裏我無法用按鈕單擊事件替換我的默認片段(包含三個按鈕)到其他片段。 PS:=>問題:我的代碼工作正常,編譯過,但按鈕沒有響應 這裏是我的代碼我不能使用按鈕單擊事件將我的默認片段(主片段)替換爲其他片段?

activity_main.xml中

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/main_container" 
     /> 


    <Button 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:text="ABOUT ME" 
     android:id="@+id/bttn_about" 
     android:layout_gravity="center_horizontal" /> 

    <Button 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:text="MY SKILLS" 
     android:id="@+id/bttn_skills" 
     android:layout_gravity="center_horizontal" /> 

    <Button 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:text="CONTACT ME" 
     android:layout_below="@+id/bttn_skills" 
     android:layout_gravity="center_horizontal" /> 
</LinearLayout> 

,這裏是我的Java文件。

mainActivity.java

公共類MainActivity擴展AppCompatActivity {

Button bttn_about,bttn_skills,bttn_contact; 
    FragmentTransaction fragmentTransaction; 

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

    fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
    fragmentTransaction.add(R.id.main_container,new HomeFragment()); 
    fragmentTransaction.commit(); 

    bttn_about=(Button)findViewById(R.id.bttn_about); 
    bttn_skills=(Button)findViewById(R.id.bttn_skills); 
    bttn_contact=(Button)findViewById(R.id.bttn_contact); 

    bttn_about.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

    //FragmentManager,Fragment Transaction..... same as above code 
     } 
    }); 

    } 

,這裏是第二個片段

<FrameLayout 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" 
    tools:context="com.example.AboutmeFragment"> 

<!-- TODO: Update blank fragment layout --> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="80dp" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="@string/aboutme" 
    android:id="@+id/textView2" 
    android:layout_gravity="left|top" /> 
    </RelativeLayout> 
    </FrameLayout> 
+0

您是否收到錯誤訊息? – Masum

+0

沒有錯誤信息,無法點擊關於按鈕。 :( – sudheer

回答

1

你沒有實際片段事務代碼替換//FragmentManager,Fragment Transaction..... same as above code

+0

fragmentTransaction = getSupportFragmentManager()。beginTransaction(); fragmentTransaction.add(R.id.main_container,new aboutFragment()); fragmentTransaction.commit(); – sudheer

+0

是的,把它放在你的'onClick'中,儘管你可能會想要使用'fragmentTransaction.replace'而不是'fragmentTransaction.add' –