我的findFragmentById方法返回null(v4)。 我想从包装器活动中调用片段函数; 像Fragment.myFunc();
这样的东西在Container活动中 import android.support.v4.app.FragmentManager; 在片段活动中 import android.support.v4.app.Fragment;
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:adroid="http://schemas.android.com/apk/res-auto"
android:id="@+id/firstFragmentWrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:fromYScale="0.0"
android:fillAfter="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
android:tag="firstFragmentWrapper">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout android:id="@+id/relativeWrapperRecoverText"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
android:layout_gravity="bottom"
android:gravity="bottom"
android:paddingBottom="0dp">
<design.ubris.myuni.UtilityAvenirNextFontTextView
android:id="@+id/recoverPLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="@string/recover_password"
android:lineSpacingExtra="4dp"
android:textSize="16dp"
android:textColor="#757575"
android:paddingLeft="5dp"
android:letterSpacing="0.2"/>
</RelativeLayout>
<RelativeLayout android:id="@+id/relativeWrapperRecover"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:paddingTop="5dp"
android:descendantFocusability="beforeDescendants">
<design.ubris.myuni.UtilityAvenirNextFontTextView
android:id="@+id/recoverLabel"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="bottom"
android:layout_marginBottom="30dp"
android:text="Codice Fiscale"
android:textSize="25dp"
android:textColor="#757575"
android:paddingLeft="5dp"
android:letterSpacing="0.2"
android:paddingBottom="10dp"
android:clickable="false"
android:longClickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textCursorDrawable="@drawable/cursor_color"
/>
<EditText
android:id="@+id/recoverEditText"
android:layout_width="match_parent"
android:layout_height="70dp"
android:backgroundTint="@color/colorPrimary"
android:textColorHint="@color/colorPrimary"
android:textColor="#fff"
android:gravity="bottom"
android:paddingBottom="20dp"
android:focusableInTouchMode="true"
android:focusable="true"
android:singleLine="true"
android:inputType="textVisiblePassword"
android:imeOptions="actionDone"
android:imeActionLabel="Login"
android:layout_alignParentRight="true"
android:textCursorDrawable="@drawable/cursor_color"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginStart="4dp"
android:layout_marginTop="85dp"
android:height="50dp"
android:background="@color/colorPrimary"
android:text="Invia richiesta"
android:textColor="#fff"
android:id="@+id/recovery_send"/>
</RelativeLayout>
</LinearLayout>
FragmentManager fm = getSupportFragmentManager();
FragmentLogin1 fragment =FragmentLogin1)fm.findFragmentById(R.id.firstFragmentWrapper);
Toast.makeText(getApplicationContext(), ""+fragment, Toast.LENGTH_LONG).show();
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {
}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
public void onPageSelected(int position) {
//>> HIDE / UNHIDE lost password
//LOST PASSWORD
switch(position){
case 0:
fSelected = 0;
lostPassword.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out));
backButton.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in));
backButton.bringToFront();
EditText recover = findViewById(R.id.recoverEditText);
if(recover.getText().toString().equals("")) {
FragmentManager fm = getSupportFragmentManager();
FragmentLogin1 fragment = (FragmentLogin1) fm.findFragmentById(R.id.firstFragmentWrapper);
Toast.makeText(getApplicationContext(), ""+fragment, Toast.LENGTH_LONG).show();
//fragment.yourPublicMethod();
}
break;
case 1:
fSelected = 1;
lostPassword.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in));
backButton.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out));
lostPassword.bringToFront();
break;
case 2:
fSelected = 2;
//>>
break;
}
//Toast.makeText(getApplicationContext(),"swipe",Toast.LENGTH_SHORT).show();
fAnimation(position);
}
});
导致nullpointer异常;没有检测到错误。
答案 0 :(得分:2)
XML中的片段使用<fragment>
XML标记。现在,您只使用普通FrameLayout
。要使用FragmentManager.findFragmentById(id)
,您必须为Fragment
提供<fragment>
标记,类名和ID。
如果您需要其他信息,可以参加相当广泛的培训article。