0
我有一個主要活動,有兩個選項卡,每個選項卡是一個片段(frag1和frag2)。 frag1的佈局有三個文本視圖和一個按鈕,frag1 實現SensorEventListener並在三個文本視圖中顯示加速度計值,該按鈕用於將z軸的加速度計值傳遞給frag2。 Frag2只有一個帶有textview的佈局來顯示來自frag1的傳遞值。通過接口的兩個片段之間的通信失敗
這兩個片段之間的通信是通過一個接口實現的,該接口由承載frag1和frag2的主要活動實現。在運行時 當我按下frag1中的按鈕將加速器值傳遞給frag2時,儘管創建了frag2並且其視圖已準備就緒,但我仍然收到getView = null。
下面我公佈的主要活動,frag1和frag2,請大家幫我看看爲什麼我每次按下按鈕時,通過從frag1到frag2東西的價值,顯示和 getView是空
主活動:
public class MainActivity extends AppCompatActivity implements IDataPasser{
private Toolbar mTB = null;
private TabLayout mTL = null;
private ViewPager mVP = null;
private VPagerAdapter mVPAdapter = null;
private Frag_2 mFrag2 = null;
private Frag_1 mFrag1 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.initViews(R.layout.activity_main);
this.initObjs();
}
private void initViews(int rootView) {
setContentView(rootView);
this.mTB = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(this.mTB);
this.mTL = (TabLayout) findViewById(R.id.tab_layout);
this.mTL.addTab(this.mTL.newTab().setText("Tab 1"));
this.mTL.addTab(this.mTL.newTab().setText("Tab 2"));
this.mTL.setTabGravity(TabLayout.GRAVITY_FILL);
this.mVP = (ViewPager) findViewById(R.id.pager);
this.mVP.setOffscreenPageLimit(1);
}
private void initObjs() {
this.mFrag2 = new Frag_2();
this.mVPAdapter = new VPagerAdapter(getSupportFragmentManager(), this.mTL.getTabCount());
this.mVP.setAdapter(this.mVPAdapter);
this.mVP.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(this.mTL));
this.mTL.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
mVP.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
@Override
public void onValChanged(float val) {
this.mFrag2.fromFrag1(val);
}
}
frag1:
public class Frag_1 extends Fragment implements SensorEventListener{
private final String TAG = this.getClass().getSimpleName();
private IDataPasser mPasser;
private View mRootView = null;
private TextView mtvAccX = null;
private TextView mtvAccY = null;
private TextView mtvAccZ = null;
private Button mbtnPass = null;
private SensorManager sensorManager;
float x;
float y;
float z;
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.w(TAG, "onAttach()");
this.mPasser = (IDataPasser) context;
sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.w(TAG, "onCreate()");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.w(TAG, "onCreateView()");
this.mRootView = inflater.inflate(R.layout.frag_1, container, false);
this.mtvAccX = (TextView) this.mRootView.findViewById(R.id.frag1_x);
this.mtvAccY = (TextView) this.mRootView.findViewById(R.id.frag1_y);
this.mtvAccZ = (TextView) this.mRootView.findViewById(R.id.frag1_z);
this.mbtnPass = (Button) this.mRootView.findViewById(R.id.frag1_btn_pass);
return this.mRootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.w(TAG, "onViewCreated()");
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.w(TAG, "onActivityCreated()");
Sensor accSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, accSensor, sensorManager.SENSOR_DELAY_FASTEST);
this.mbtnPass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPasser.onValChanged(z);
}
});
}
@Override
public void onStart() {
super.onStart();
Log.w(TAG, "onStart()");
}
@Override
public void onResume() {
super.onResume();
Log.w(TAG, "onResume()");
}
@Override
public void onPause() {
super.onPause();
Log.w(TAG, "onPause()");
}
@Override
public void onStop() {
super.onStop();
Log.w(TAG, "onStop()");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.w(TAG, "onDestroy()");
}
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
float[] values = event.values;
x = values[0];
y = values[1];
z = values[2];
this.mtvAccX.setText(String.valueOf(x));
this.mtvAccY.setText(String.valueOf(y));
this.mtvAccZ.setText(String.valueOf(z));
break;
default:
break;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
frag2:
public class Frag_2 extends Fragment {
private final String TAG = this.getClass().getSimpleName();
private View mRootView = null;
private TextView mtvText = null;
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.w(TAG, "onAttach()");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.w(TAG, "onCreate()");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.w(TAG, "onCreateView()");
this.mRootView = inflater.inflate(R.layout.frag_2, container, false);
this.mtvText = (TextView) this.mRootView.findViewById(R.id.frag2_tv_z);
Log.v(TAG, "getView(): " + getView());
return mRootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.w(TAG, "onViewCreated()");
Log.v(TAG, "getView(): " + getView());
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.w(TAG, "onActivityCreated()");
}
@Override
public void onStart() {
super.onStart();
Log.w(TAG, "onStart()");
}
@Override
public void onResume() {
super.onResume();
Log.w(TAG, "onResume()");
}
@Override
public void onPause() {
super.onPause();
Log.w(TAG, "onPause()");
}
@Override
public void onStop() {
super.onStop();
Log.w(TAG, "onStop()");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.w(TAG, "onDestroy()");
}
public void fromFrag1(float val) {
if (getView() != null) {
this.mtvText.setText(""+val);
} else {
Log.e(TAG, "getView = null");
}
}
}
'this.mFrag2 =新Frag_2();'。 'mFrag2'與'ViewPager'中加載的實例不同。獲取加載的實例,一切都會好的。 – Rohit5k2
@ Rohit5k2可以請你告訴我如何讓他在觀看者中扮演的角色 – user2121
請參閱http://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager/24662049#24662049快樂路徑部分 – Rohit5k2