Reputation: 103
my findFragmentById method return always null (v4). i wanto to call a fragment function from wrapper activity; somthing like Fragment.myFunc();
in Container activity import android.support.v4.app.FragmentManager; in fragment activity 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);
}
});
that result in a nullpointer exception; no error detected.
Upvotes: 0
Views: 51
Reputation: 8870
Fragment's in XML use the <fragment>
XML tag. Right now, you're using just a normal FrameLayout
. In order to use FragmentManager.findFragmentById(id)
, you have to provide the Fragment
with a <fragment>
tag, class name, and id.
There is a pretty extensive training article on this if you need additional info.
Upvotes: 2