2

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

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)

谢谢你的帮助...

4

2 回答 2

5

You are trying to add a fragment to your Buttons which does not work, because as the Exception says, Buttons aren't ViewGroups, so that can't have other Views inside. So the exceptions are caused by the R.id.colorBlack - the ID here should be the ID of a ViewGroup, e.g. a LinearLayout

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

                     transaction.commit();

Where do you want your FragmentAferin to be placed?

于 2013-03-23T12:49:18.707 回答
0

你的课应该扩展FragmentActivity而不是Activity

您可以从Android 支持库中获取此类

于 2013-03-23T11:12:27.750 回答