java.lang.ClassCastException:android.widget.Button无法强制转换为android.view.ViewGroup

时间:2013-03-23 11:02:10

标签: android android-fragments

我是片段新手。 ı做了一个简单的片段,但无法在没有崩溃的情况下工作...... 当按下按钮开始片段时它崩溃 ...我看起来好片段学习源并等待你的建议.. 谢谢你的关注... 这是主要的活动;

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)

谢谢你的帮助......

2 个答案:

答案 0 :(得分:5)

您正在尝试将片段添加到您的按钮中,但这不起作用,因为正如Exception所说,按钮不是ViewGroups,因此不能在其中包含其他视图。 因此,例外是由R.id.colorBlack引起的 - 这里的ID应该是ViewGroup的ID,例如一个LinearLayout

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

                     transaction.commit();

您希望FragmentAferin放在哪里?

答案 1 :(得分:0)

您的课程应该延伸FragmentActivity而不是Activity

您可以从Android support library

获取此课程