2013-03-23 103 views
2

我是新的碎片。 ı做了一个简单的片段,但不能管理没有崩溃的工作... 当我按按钮开始碎片它是崩溃 ...我lookig为好片段学习来源和等待您的建议.. 感谢你的兴趣... 这里是主要的活动;java.lang.ClassCastException:android.widget.Button不能转换为android.view.ViewGroup

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

} 

这里是主要的XML布局;

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

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

    <Button 
     android:id="@+id/colorRed" 
     style="@style/FragmentTextTheme" 
     android:background="@color/Red" 
     android:text="@string/red" 
     android:textColor="@android:color/white" /> 
    <Button 
     android:id="@+id/colorBlue" 
     style="@style/FragmentTextTheme" 
     android:background="@color/Blue" 
     android:text="@string/blue" 
     android:textColor="@android:color/white" /> 

    </LinearLayout> 

这里是片段类代码;

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

这里是片段XML布局;

<?xml version="1.0" encoding="utf-8"?> 
<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" > 

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

</LinearLayout> 

例外

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) 

感谢您的帮助...

+0

尝试清洁您的项目 – 2013-03-23 11:06:46

+0

ı做到了,但仍然是异常... – hopeTo 2013-03-23 12:34:56

回答

5

得到这个类您正在尝试添加一个片段到您的按钮不起作用,因为如异常说,按钮不是ViewGroups,所以不能有其他视图里面。 因此,异常是由R.id.colorBlack引起的 - 这里的ID应该是ViewGroup的ID,例如, LinearLayout

FragmentTransaction transaction=manager.beginTransaction(); 
        transaction.add(R.id.colorBlack, new FragmentAferin()); 

        transaction.commit(); 

您想在哪里放置您的FragmentAferin

相关问题