我试图让我的应用程序从我的片段活动中的片段类中调用函数。但是,我在找到片段时遇到了很多问题。现在我可以通过 id 找到一个片段,我认为它是托管选项卡的片段。但是当我得到子片段管理器时,我得到了一个空值。任何人都可以帮忙吗?
这是片段活动
package com.example.profileactivity;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TabHost.TabSpec;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
public class ProfileActivity extends FragmentActivity {
// Fragment TabHost as mTabHost
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this,getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("user").setIndicator("User",
getResources().getDrawable(R.drawable.ic_user_tab)),
UserProfileTab.class,null);
mTabHost.addTab(mTabHost.newTabSpec("payment").setIndicator("Payment",
getResources().getDrawable(R.drawable.ic_payment_tab)),
PaymentInfoTab.class,null);
}
public void buttonClick(View view){
FragmentManager fm = getSupportFragmentManager();
if(fm.findFragmentById(R.id.realtabcontent) == null){
Log.d("TABHOST", "didnt find fragment");
}
else{
Log.d("TABHOST", "found fragment");
//PaymentInfoTab paymentTab = (PaymentInfoTab)fm.findFragmentByTag("payment");
//paymentTab.boom();
if(fm.findFragmentById(R.id.realtabcontent).getChildFragmentManager().findFragmentByTag("payment") != null){
PaymentInfoTab paymentTab = (PaymentInfoTab)fm
.findFragmentById(R.id.realtabcontent)
.getChildFragmentManager()
.findFragmentByTag("payment");
paymentTab.boom();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.profile, menu);
return true;
}
}
这是片段活动的 xml。
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>