0

myFragment and myFragment2 both return null for below code. Not sure what the problem is. This solution didn't solve the problem unfortunately, it is not a duplicate.

MainActivity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    NonceInputFragment nonceInputFragment = new NonceInputFragment();
    transaction.add(R.id.nonce_input_fragment,nonceInputFragment,"test");
    transaction.commit();

    // It crashes on this line:
    getSupportFragmentManager().executePendingTransactions();

    NonceInputFragment myFragment = (NonceInputFragment) getSupportFragmentManager().findFragmentById(R.id.nonce_input_fragment);

    NonceInputFragment myFragment2 = (NonceInputFragment) getSupportFragmentManager().findFragmentByTag("test");
    }
}

nonce_input.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/nonce_input_fragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/tvLabel"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

NonceInputFragment

public class NonceInputFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.nonce_input, container, false);
        return view;
    }
}

Error message:
java.lang.RuntimeException: Unable to start activity ComponentInfo{name.com.viewpagercode/name.com.viewpagercode.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0b0056 (name.com.viewpagercode:id/nonce_input_fragment) for fragment NonceInputFragment{cece542 #0 id=0x7f0b0056 test}

Community
  • 1
  • 1
Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94
  • `FragmentTransaction`s happen asynchronously, so they will not have completed by the time you're trying to find your `Fragment` in that code. If you really need to get a reference to the `Fragment` in `onCreate()`, call `executePendingTransactions()` on the support `FragmentManager` right after the `commit()` call. – Mike M. Aug 05 '16 at 09:53
  • @MikeM. Adding executePendingTransactions makes the app crash. – Jim Clermonts Aug 05 '16 at 11:45
  • 1
    It's still a duplicate, you've just got another problem now. I don't know what exactly you mean in the comment on Adam's answer, but he's correct (except for the layout name mismatch, now that you've changed things). The ID you pass in the `add()` call needs to be the ID of a `ViewGroup` in the `Activity`'s layout, not in the `Fragment`'s layout. – Mike M. Aug 05 '16 at 12:05
  • 1
    @MikeM. you're right, I forgot the fragment had to belong to an Activity. – Jim Clermonts Aug 06 '16 at 12:55

1 Answers1

0

Your layout R.layout.identify_fragment does not have a view with android:id="@+id/nonce_input_fragment".

Specifically, this line:

transaction.add(R.id.nonce_input_fragment,nonceInputFragment,"test");

is saying "add this fragment to this activity, and put it in the container whose id is nonce_input_fragment. The exception indicates that no such view exists in your activity - meaning your layout (identify_fragment) needs to add something like this:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/nonce_fragment" />
Adam S
  • 16,144
  • 6
  • 54
  • 81
  • identify_fragment.xml (stupid name, should be main_activity.xml) belongs to MainActivity. I need to call a function in NonceInputFragment, which belongs to a different Activity. NonceInputFragment is not part of identify_fragment.xml – Jim Clermonts Aug 05 '16 at 11:37