title: java.lang.ClassCastException: android.widget.Button cannot be cast to android.view.ViewGroup
body: <p>I am new at fragments. ı made a simple fragment but couldn't manage to work without crash...<strong>When ı press buttons for to start fragments it is crashing</strong>... I am lookig for good fragment learning sources and waiting your advices..
  Thanks for your interest...
  Here is the main activity;</p>
  
  <pre><code>package com.example.fragments;
  
  
  import android.annotation.TargetApi;
  import android.app.Activity;
  import android.app.FragmentManager;
  import android.app.FragmentTransaction;
  import android.media.MediaPlayer;
  import android.os.Build;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.View;
  import android.widget.Button;
  
  public class Main extends Activity {
  
      MediaPlayer sound;
      Button black,red,blue;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          sound = MediaPlayer.create(Main.this, R.raw.alkis);
          black=(Button)findViewById(R.id.colorBlack);
          red=(Button)findViewById(R.id.colorRed);
          blue=(Button)findViewById(R.id.colorBlue);
  
          black.setOnClickListener(new View.OnClickListener() {
  
              @TargetApi(Build.VERSION_CODES.HONEYCOMB)
              public void onClick(View v) {
                  FragmentManager manager = getFragmentManager();
                  sound.start();
                  FragmentTransaction transaction=manager.beginTransaction();
                           transaction.add(R.id.colorBlack, new FragmentAferin());
  
                           transaction.commit();
  
  
              }
          });
          red.setOnClickListener(new View.OnClickListener() {
  
              @TargetApi(Build.VERSION_CODES.HONEYCOMB)
              public void onClick(View v) {
                  FragmentManager manager = getFragmentManager();
                  sound.start();
                  FragmentTransaction transaction = manager.beginTransaction();
                  transaction.add(R.id.colorRed, new FragmentAferin());
  
                  transaction.commit(); 
  
              }
          });
          blue.setOnClickListener(new View.OnClickListener() {
  
              @TargetApi(Build.VERSION_CODES.HONEYCOMB)
              public void onClick(View v) {
                  FragmentManager manager = getFragmentManager();
                  sound.start();
                  FragmentTransaction transaction = manager.beginTransaction();
                  transaction.add(R.id.colorBlue, new FragmentAferin());
  
                  transaction.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;
      }
  
  }
  </code></pre>
  
  <p>and here is the main XML Layout;</p>
  
  <pre><code>   &lt;LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
           tools:context=".Main" &gt;
  
      &lt;Button
          android:id="@+id/colorBlack"
          style="@style/FragmentTextTheme"
          android:background="@android:color/black"
          android:gravity="center"
          android:clickable="true"
          android:onClick="@string/black"
          android:text="@string/black"
          android:textColor="@android:color/white" /&gt;
  
      &lt;Button
          android:id="@+id/colorRed"
          style="@style/FragmentTextTheme"
          android:background="@color/Red"
          android:text="@string/red" 
          android:textColor="@android:color/white" /&gt;
      &lt;Button
          android:id="@+id/colorBlue"
          style="@style/FragmentTextTheme"
          android:background="@color/Blue"
          android:text="@string/blue" 
          android:textColor="@android:color/white" /&gt;
  
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>and here is the fragment class code;</p>
  
  <pre><code>package com.example.fragments;
  
  import android.annotation.TargetApi;
  import android.app.Fragment;
  import android.os.Build;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  public class FragmentAferin extends Fragment {
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
  
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_aferin,
                  container, false);
  
          return view;
      }
  </code></pre>
  
  <p>and here is the fragment XML layout;</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_gravity="center"
      android:gravity="center"
      android:orientation="vertical" &gt;
  
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:text="@string/aferin"
          android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>exception</p>
  
  <pre><code> FATAL EXCEPTION: main
  java.lang.ClassCastException: android.widget.Button cannot be cast to android.view.ViewGroup
      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:875)
      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
      at android.app.BackStackRecord.run(BackStackRecord.java:682)
      at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
      at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
      at android.os.Handler.handleCallback(Handler.java:725)
      at android.os.Handler.dispatchMessage(Handler.java:92)
      at android.os.Looper.loop(Looper.java:137)
      at android.app.ActivityThread.main(ActivityThread.java:5039)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:511)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
      at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>thank you for your helps...</p>