我是新的碎片。 ı做了一個簡單的片段,但不能管理沒有崩潰的工作... 當我按按鈕開始碎片它是崩潰 ...我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)
感謝您的幫助...
嘗試清潔您的項目 – 2013-03-23 11:06:46
ı做到了,但仍然是異常... – hopeTo 2013-03-23 12:34:56