在FragmentActivity中启动类时出错(java.lang.IllegalArgumentException:找不到id的视图)

时间:2013-07-30 17:49:52

标签: android android-fragments bluetooth android-fragmentactivity

我正在构建一款具有游戏的应用程序,并通过蓝牙将游戏信息传达给第二台无线多人游戏设备。现在我正在尝试将蓝牙和游戏类放在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部分(Not Not My code)所以不要告诉我我缺少大括号。

  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);
        }
    }

1 个答案:

答案 0 :(得分:1)

设备是否处于横向模式?

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

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