我有一个问题,FragmentActivity
平板电脑上的 a 加载Fragment
view
但不显示它,除非它与之交互。因此,视图是通过onActivityResult()
函数中的主要活动加载的,加载中的所有代码都Fragment
很好,因为我可以看到 log cat 记录了所有信息。但由于某种原因,它Fragment
是不可见的,但如果我按下有按钮的区域,即后退按钮,Fragment
则会出现然后消失(如果后退按钮被称为应该)。奇怪的事件是,如果我将片段取出onActivityResult()
并按下按钮调用它,它工作正常,所以我知道它不是片段中的 xml 问题或 java 问题。我认为是onActivityResult()
功能,但我想不通的是,当我开始开发这个应用程序时,我按照我的方式实现它,它工作得很好。
该函数的代码位于该函数中导致问题的片段下方,应用程序中的所有其他片段都工作正常。另一件事是该应用程序(包括此函数中的片段)在手机上运行良好,因此这也向我表明这是一个平板电脑问题(也在两个不同的平板电脑上进行了测试,运行 2.2.3 和 nexus 7 的三星 Galaxy Tab运行 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) {
}
}
//编辑
好的,所以我做了一个小发现,片段加载正常,但我发现它加载在主片段后面(xml 中设置的一个,如下所示),但仅在从onActivityResult()
函数调用时才加载。该函数指向,headlines_fragment_two
但如果我将它指向headlines_fragment
前面的片段加载。现在我已经更改了headlines_fragment_two
from的名称,activity_fragment
看看这是否会有所作为(遗憾的是它没有)。因此,如果有人对为什么会发生这种情况有任何想法,请提供帮助。平板上片段布局的xml代码如下:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="@+id/headlines_fragment"
android:name="com.package.name.MainFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.99" />
<RelativeLayout
android:id="@+id/divider"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.01"
android:background="#A6B5FAD9" />
<fragment
android:id="@+id/headlines_fragment_two"
android:name="com.package.name.ListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>