2016-04-30 86 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' –