0

我正在构建一个应用程序,该应用程序具有游戏并通过蓝牙将游戏信息传送到第二台设备以进行无线多人游戏。现在我正在尝试将蓝牙和游戏类放在 FragmentActivity 类中,以便在玩游戏时实现无缝蓝牙访问。我能够成功地通过片段加载游戏,但由于某种原因,蓝牙移植会引发错误。我尝试过使用我通常使用的 activity.setContentView() 和 inflater.inflate,但我仍然遇到持续错误。有人可以告诉我如何解决我的问题吗> 我已经包含了有问题的代码和错误日志输出。

07-30 13:27:25.225  15076-15076/com.example.android.BluetoothChat E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.BluetoothChat/com.example.android.BluetoothChat.FragmentHolder}: java.lang.IllegalArgumentException: No view found for id 0x7f060001 for fragment BluetoothChat{41b2d4e0 #1 id=0x7f060001}
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
        at android.app.ActivityThread.access$600(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        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:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f060001 for fragment BluetoothChat{41b2d4e0 #1 id=0x7f060001}
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:823)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
        at android.app.BackStackRecord.run(BackStackRecord.java:635)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
        at android.app.Activity.performStart(Activity.java:5017)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
        ... 11 more
07-30 13:28:51.565  13981-13981/? E/APKInstallReceiver: context = android.app.ReceiverRestrictedContext@41998db0, intent.getAction() = android.intent.action.PACKAGE_REMOVED, intent.getDataString() = package:com.example.android.BluetoothChat
07-30 13:28:51.825  13981-13981/? E/APKInstallReceiver: context = android.app.ReceiverRestrictedContext@41998db0, intent.getAction() = android.intent.action.PACKAGE_ADDED, intent.getDataString() = package:com.example.android.BluetoothChat
07-30 13:28:56.305  15215-15215/? E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.BluetoothChat/com.example.android.BluetoothChat.FragmentHolder}: java.lang.IllegalArgumentException: No view found for id 0x7f060001 for fragment BluetoothChat{418a7730 #1 id=0x7f060001}
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
        at android.app.ActivityThread.access$600(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        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:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f060001 for fragment BluetoothChat{418a7730 #1 id=0x7f060001}
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:823)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
        at android.app.BackStackRecord.run(BackStackRecord.java:635)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
        at android.app.Activity.performStart(Activity.java:5017)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
        ... 11 more



 View view;
    Activity activity;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    if(D) Log.e(TAG, "+++ ON CREATE +++");
    activity = getActivity();
    // Set up the window layout
//        activity.setContentView(R.layout.main);

    view = inflater.inflate(R.layout.main,
            container, false);



    // Get local Bluetooth adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    // If the adapter is null, then Bluetooth is not supported
    if (mBluetoothAdapter == null) {
        Toast.makeText(activity, "Bluetooth is not available", Toast.LENGTH_LONG).show();
        startActivity(new Intent(activity, StartGame.class));
//            this.finish();

    }
    return view;
}

这是 FragmenActivity 部分(不是所有我的代码,所以不要告诉我我缺少大括号。

  public class FragmentHolder extends FragmentActivity {

    public static boolean menuHidden = true;
    public static final int LEFT_FRAG = R.id.fragmentcontainerleft;
    public static final int RIGHT_FRAG = R.id.fragmentcontainerright;
    public static final int BOTTOM_FRAG = R.id.fragmentcontainerbottom;
    Fragment menu, main, top;

    View view;
    Activity activity;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.bluetooth_menu_frag);

        if(savedInstanceState == null){
            FragmentManager manager = getFragmentManager();

            //Start transaction
            FragmentTransaction transaction = manager.beginTransaction();

            //Create fragments
            menu = new BluetoothChat();
            main = new StartGame();
    //            top = new TopFragment();

            //Add fragments
            transaction.add(RIGHT_FRAG,main);
            transaction.add(LEFT_FRAG,menu );
    //            transaction.add(BOTTOM_FRAG,top);

            transaction.commit();
        }

        if(!menuHidden){
            ScrollView scrollview = (ScrollView)findViewById(R.id.fragleft);
            scrollview.setVisibility(View.VISIBLE);
            FrameLayout menuFrame = (FrameLayout)findViewById(R.id.fragmentcontainerleft);
            menuFrame.setVisibility(View.VISIBLE);
        }
    }
4

1 回答 1

1

设备是否处于横向模式?

如果是这样,res 文件夹中的视图是布局还是土地布局?我发现这可以完全解决这些错误。

我认为这是 Android 寻找资源时的时间问题。如果当前布局是横向的,它将首先在那里查找资源,然后再查找其他文件夹。不幸的是,资源中必须有两个完全相同的文件副本,但这是我找到解决问题的唯一方法。

于 2013-09-27T17:22:21.287 回答