我遇到了一个问题,平板电脑上的FragmentActivity
加载Fragment
view
,但除非与其进行交互,否则不会显示它。因此,通过onActivityResult()
函数中的主要活动加载视图,Fragment
内的所有代码都可以正常加载,因为我可以看到日志猫记录了所有信息。但由于某种原因,Fragment
不可见,但如果我按下的区域中有一个按钮,即后退按钮,Fragment
出现,然后消失(如果后退按钮被调用就像它应该)。事件陌生的事情是,如果我从onActivityResult()
中取出片段并在按钮按下时调用它,它工作正常,所以我知道它不是片段中的xml问题或java问题。我认为它是onActivityResult()
函数,但我无法弄清楚的是,当我开始开发这个应用程序,并且按照我的方式实现它时,它运行良好。片段载入问题android
该函数的代码位于该函数内导致问题的片段之下,应用程序内的所有其他片段都正常工作。另一件事是该应用程序(包括此功能中的片段)在手机上运行良好,这也表明它的平板电脑问题(也在两个不同的平板电脑上测试过,运行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
的名称从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>
你对不同的分辨率使用不同的布局吗?问题可能在于为更高分辨率(平板电脑)构建布局。 –
好,所以我发现上面的代码,由于某种原因,使碎片加载在主要片段后面(通过主要片段我的意思是我在xml文件中为第一次加载设置的片段)。我将编辑数据以显示xml片段布局 –