片段加载问题android

时间:2013-10-31 15:53:34

标签: android android-fragments fragment onactivityresult

我有一个问题,即平板电脑上的FragmentActivity加载Fragment view,但除非与之互动,否则不会显示该问题。所以视图是通过onActivityResult()函数中的主要活动加载的,Fragment中的所有代码都加载正常,因为我可以看到log cat记录了所有信息。但由于某些原因,Fragment不可见,但如果我按下有按钮的区域,即后退按钮,Fragment会出现然后消失(如果后退按钮被调用,则这应该)。事件更奇怪的是,如果我从onActivityResult()中取出片段并在按下按钮中调用它,它工作正常,所以我知道它不是片段中的xml问题或java问题。我认为是onActivityResult()函数,但我无法弄清楚的事实是,当我开始开发这个应用程序时,我按照我的方式实现它,它运行正常。

该函数的代码低于此函数中导致问题的片段,应用程序中的所有其他片段都正常工作。另一件事是应用程序(包括此功能中的片段)在手机上工作正常,这也向我表明它是一个平板电脑问题(也在两个不同的平板电脑上测试,三星Galaxy标签运行2.2.3和nexus 7运行4.3),所以如果有人可以帮助我,我会非常感激:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    System.out.println("ACTIVITY RESULT");
    System.out.println("REQUEST CODE >>>>> " + requestCode);

    if (requestCode == webview) {

        if (resultCode == RESULT_OK) {

            System.out.println("RESULT WEBVIEW");

            System.out.println(data.getStringExtra("URL"));

            Bundle args = new Bundle();
            args.putString("URL", data.getStringExtra("URL"));
            args.putString("List Name", currentList);
            args.putInt("List Position", pos);

            Fragment newFrag = new URLFragment();

            // newFrag.setArguments(args);
            FragmentTransaction transaction = MainActivity.this
                    .getSupportFragmentManager().beginTransaction();
            transaction.setCustomAnimations(R.anim.in_from_right,
                    R.anim.out_from_left);

            System.out.println("FRAG CONTAINER >>>> "
                    + findViewById(R.id.fragment_container));
            System.out.println("ARTICLE FRAG >>>> "
                    + findViewById(R.id.article_fragment));

            if (MainActivity.this.findViewById(R.id.fragment_container) != null) {
                transaction.add(R.id.fragment_container, newFrag,
                        "URL Fragment");
                transaction.addToBackStack(null);
            } else {
                System.out.println("IS THIS TABLET?");

                transaction.add(R.id.article_fragment, newFrag,
                        "URL Fragment");
            }

            transaction.commit();

        }
        if (resultCode == RESULT_CANCELED) {
            // Write your code if there's no result
            System.out.println("CANCELED WEBVIEW");
        }
    } else if (requestCode == camera) {

        if (resultCode == RESULT_OK) {

            System.out.println("RESULT CAMERA");
            System.out.println(data.getData());

            if (data.getData().toString().contains("picasa")) {
                System.out.println("UNABLE TO LOAD IMAGE");
                Toast.makeText(
                        getApplicationContext(),
                        "We are unable to load this image, please select a different one",
                        Toast.LENGTH_LONG).show();
            } else {

                if (getSupportFragmentManager().findFragmentByTag(
                        "Image Fragment") == null) {
                    GatherGalleryImages gatherImage = new GatherGalleryImages();
                    Uri selectedImageUri = data.getData();
                    String[] projection = { MediaStore.Images.Media.DATA };
                    Cursor cursor = managedQuery(selectedImageUri,
                            projection, null, null, null);

                    String selectedImagePath = gatherImage.getPath(cursor);

                    System.out.println(selectedImagePath);

                    Bundle args = new Bundle();
                    args.putString("List Name", currentList);
                    args.putInt("List Position", pos);
                    args.putString("Image Location", selectedImagePath);

                    CameraItemFragment newFragment = new CameraItemFragment();
                    newFragment.setArguments(args);
                    FragmentTransaction transaction = getSupportFragmentManager()
                            .beginTransaction();
                    transaction.setCustomAnimations(R.anim.in_from_right,
                            R.anim.out_from_left);

                    if (findViewById(R.id.fragment_container) != null) {
                        transaction.add(R.id.fragment_container,
                                newFragment, "Image Fragment");
                        transaction.addToBackStack(null);
                        transaction.commit();
                    } else {

                        transaction.add(R.id.article_fragment, newFragment,
                                "Image Fragment");
                        // transaction.addToBackStack(null);
                        transaction.commit();
                    }
                }

            }
        }
        if (resultCode == RESULT_CANCELED) {
            // Write your code if there's no result
            System.out.println("CANCELED CAMERA");
        }
    } else if (requestCode == IntentIntegrator.REQUEST_CODE) {

        if (resultCode != RESULT_CANCELED) {
            IntentResult scanResult = IntentIntegrator.parseActivityResult(
                    requestCode, resultCode, data);
            if (scanResult != null) {
                String upc = scanResult.getContents();

                // put whatever you want to do with the code here
                System.out.println("UPC >>>> " + upc);
                Intent mainIntent = new Intent(MainActivity.this,
                        Webview_Activity.class);
                mainIntent.putExtra("upc", upc);
                mainIntent.putExtra("listName", currentList);

                MainActivity.this.startActivityForResult(mainIntent, 1);
            }
        }
    } else if (requestCode == cam) {
        System.out.println("CAMERA INTENT 0");

        if (resultCode == RESULT_OK) {

            Bitmap photo = (Bitmap) data.getExtras().get("data");

            // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
            Uri tempUri = getImageUri(getApplicationContext(), photo);

            // CALL THIS METHOD TO GET THE ACTUAL PATH
            File finalFile = new File(getRealPathFromURI(tempUri));

            String selectedImagePath = finalFile.getAbsolutePath();

            Bundle args = new Bundle();
            args.putString("List Name", currentList);
            args.putInt("List Position", pos);
            args.putString("Image Location", selectedImagePath);

            CameraItemFragment newFragment = new CameraItemFragment();
            newFragment.setArguments(args);
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();
            transaction.setCustomAnimations(R.anim.in_from_right,
                    R.anim.out_from_left);

            if (findViewById(R.id.fragment_container) != null) {
                transaction.add(R.id.fragment_container, newFragment,
                        "Image Fragment");
                transaction.addToBackStack(null);
                transaction.commit();
            } else {

                transaction.add(R.id.article_fragment, newFragment,
                        "Image Fragment");
                // transaction.addToBackStack(null);
                transaction.commit();
            }
        }

        if (resultCode == RESULT_CANCELED) {
            // Write your code if there's no result
        }
    } else if (resultCode == RESULT_CANCELED) {

    }

}

// EDIT

好的,所以我做了一点发现,片段加载得很好,但是我发现它正在主片段后面加载(xml中设置的片段,如下所示),但只有从{{1功能。该函数指向onActivityResult(),但如果我将其指向headlines_fragment_two,则片段会在前面加载。现在我已经从headlines_fragment更改了headlines_fragment_two的名称,看看是否会产生任何影响(遗憾的是它没有)。因此,如果有人有任何想法为什么会这样,请帮助。平板电脑上片段布局的xml代码如下:

activity_fragment

1 个答案:

答案 0 :(得分:0)

解决了这个问题,在我的一个片段中,我有一个调用listFragment的onResume()函数,由于某些原因导致我出现问题