6

我正在处理片段和..

我的代码中有一个我找不到的问题。

logcat 在我的一个片段中指向这段代码:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_main, container, false);
}

我的主要课程(FragmentActivity):

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

public class Fragments extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragments);

        MainActivity fragment = new MainActivity();
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.add(R.id.fragment_place, fragment);
        transaction.commit();
    }

    public void onSelectFragment(View view) {

        Fragment newFragment;

        if (view == findViewById(R.id.add)) {
            newFragment = new Add();
        } else if (view == findViewById(R.id.map)) {
            newFragment = new MainActivity();
        } else {
            newFragment = new MainActivity();
        }

        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.replace(R.id.fragment_place, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

}

和日志猫:

09-30 00:02:52.363: E/AndroidRuntime(32284): FATAL EXCEPTION: main
09-30 00:02:52.363: E/AndroidRuntime(32284): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.free/com.example.free.Fragments}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.app.ActivityThread.access$600(ActivityThread.java:142)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.os.Looper.loop(Looper.java:137)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.app.ActivityThread.main(ActivityThread.java:4931)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at java.lang.reflect.Method.invokeNative(Native Method)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at java.lang.reflect.Method.invoke(Method.java:511)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at dalvik.system.NativeStart.main(Native Method)
09-30 00:02:52.363: E/AndroidRuntime(32284): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at com.example.free.MainActivity.onCreateView(MainActivity.java:124)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.app.Activity.performStart(Activity.java:5018)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
09-30 00:02:52.363: E/AndroidRuntime(32284):    ... 11 more
09-30 00:02:52.363: E/AndroidRuntime(32284): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
09-30 00:02:52.363: E/AndroidRuntime(32284):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
09-30 00:02:52.363: E/AndroidRuntime(32284):    ... 24 more

“MainActivity”类很长,所以我使用 pasebin 粘贴它:

http://pastebin.com/Lt3wbNzD

感谢你的协助!

编辑:

private GoogleMap map;

FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.map, map).commit();

它显示一个错误:

FragmentTransaction 类型中的方法 add(int, Fragment) 不适用于参数 (int, GoogleMap)

4

3 回答 3

3
Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment

您不能使用 xml 在片段内创建片段。您尚未发布该片段的 xml,但我猜您那里有一个地图片段。您仍然可以添加它,但您需要使用动态添加它getChildFragmentManager()

于 2013-09-29T21:24:40.607 回答
2

使用支持库,您应该在 xml 中使用 FrameLayout 作为片段持有者,而不是像这样的片段:

<FrameLayout android:id="@+id/fragment_place" ... />
于 2013-09-29T22:15:31.000 回答
0

你必须使用那个

return inflater.inflate(R.layout.activity_main,null);

你没有父母,因为它是片段交易..

于 2013-09-29T21:40:48.790 回答