0

I need to take data (few Strings) from Fragment in Activity (using ViewPager). I made method in this fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    v = inflater.inflate(R.layout.fragment_fragment_intro_activity_2, container, false);
    ed_operetka = v.findViewById(R.id.edOperetka);
    return v;
}
public String getText2(){
    return ed_operetka.getText().toString();
}

And i called it in Activity:

  @Override
            public void onClick(View v) {
                if (viewPager.getCurrentItem()==0) {
                viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
                    showingFirst = false;
            }else {startActivity(intent);
            String z = frag.getText2();  //line 44
                    Toast.makeText(IntroActivity.this, "" + z, Toast.LENGTH_SHORT).show();
                }
            }
        }); 

But it throws me an error:

W/RenderThread: type=1400 audit(0.0:3989185): avc: denied { read } for name="u:object_r:vendor_default_prop:s0" dev="tmpfs" ino=14894 scontext=u:r:untrusted_app:s0:c114,c256,c512,c768 tcontext=u:object_r:vendor_default_prop:s0 tclass=file permissive=0
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.wynagrodzeniedodatkowe, PID: 26732
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.wynagrodzeniedodatkowe.fragments.fragment_intro_activity_2.getText2()' on a null object reference
        at com.example.wynagrodzeniedodatkowe.IntroActivity$1.onClick(IntroActivity.java:44)

I read about interfaces, I know how to use them when the button is part of the fragment. But if its not I have no idea. Could you help me? Or give good interfaces tutorial.

EDIT

 ViewPager viewPager;
    Button button_next;
    boolean showingFirst;
    fragment_intro_activity_2 frag;
    String z;


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

        FullScreencall();

        button_next = findViewById(R.id.btnNext);
        viewPager = findViewById(R.id.viewPager);
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
        frag = new fragment_intro_activity_2();

        showingFirst = true;
        button_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewPager.getCurrentItem()==0) {
                    viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
                    showingFirst = false;
                }else {
                    z = frag.getText2();  //line 44
                    Toast.makeText(IntroActivity.this, "" + z, Toast.LENGTH_SHORT).show();
                }
            }
        });

2 Answers2

0

Okay, so the thing from what I understand is, you need to access a member (textview) of the Fragment in the parent Activity.

Now, the issue with the following code :

frag = new fragment_intro_activity_2();

is, this only creates an instance of the fragment class but DOES NOT populate the view etc. So, it is obvious you are getting nullpointer when trying to access any view of that fragment. The view is populated only when fragment is initiated via the normal flow.

So, to solve your problem, you have two choices.

1) When your fragment calls oncreate when you add it to the viewpager, you can pass the text value of that textview to the parent activity via interface and then use it in the activity. Basically, creating a link like this : FRAGMENT-> INTERFACE (text value) --> ACTIVITY

2) There is a way to access the specific fragment of a viewpager (this fragment has already been populated etc) and then you can have a public method in the fragment class to access the text (like in your original code). For this option, check this link : Getting the current Fragment instance in the viewpager

Azhar92
  • 923
  • 2
  • 8
  • 17
0

The best thing is to pass data using Intent. Here is a simple demo

From Fragment:

Intent intent =new Intent(getActivity(),ActivityName.class);
in.putExtra("const", yourValue);
startActivity(in);

From Activity:

if(getIntent().getExtra != null){
String value = Intent.getExtra("const");
}

Now you can use the string that you send through fragment in the fragment. Remove your intent onStop() or in onDestroy() method

 getIntent().removeExtra("const")
Ashim Ghimire
  • 125
  • 1
  • 10