2016-04-30 1 views
-2

私はAndroidに慣れていないし、私の断片プロジェクトに苦労している、plsは私を助ける! ここでは、私のデフォルトのフラグメント(3つのボタンを含む)をボタンのクリックイベントを使って他のフラグメントに置き換えます。 PS:=>問題:私のコードは正常に動作し、あまりにもコンパイルしますが、ボタンはここ に応答していないボタンクリックイベントを使用して、デフォルトのフラグメント(ホームフラグメント)を他のフラグメントに置き換えることはできませんか?

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

activity_main.xml私のコード

だとここに私の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 
     } 
    }); 

    } 

を拡張し、ここでは、実際のフラグメントトランザクションコードで//FragmentManager,Fragment Transaction..... same as above codeに代わるものではありませんでした第2のフラグメント

<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

です。

+0

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

+0

はい、あなたの 'onClick'の中に入れてください'fragmentTransaction.add'の代わりに' fragmentTransaction.replace'を使いたい –

関連する問題