0

I have Activity that contains custom ActionBar and ViewPager with three tabs.

I am not able to read text from EditText inside tab when click on button in ActionBar.

EDITED: Contains TABS...

public class TranAdd extends FragmentActivity...{
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
EditText et;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_pager);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    ActionBar mActionBar = getActionBar();
    mActionBar.setDisplayShowHomeEnabled(false);
    mActionBar.setDisplayShowTitleEnabled(false);
    LayoutInflater mInflater = LayoutInflater.from(this);

    et = (EditText) mViewPager.findViewById(R.id.editText);
    //...reference null...

    View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
    TextView textViewTitle = (TextView) mCustomView.findViewById(R.id.textViewTitle);
    textViewTitle.setText("New Transaction");

    ImageView imageViewOk = (ImageView) mCustomView.findViewById(R.id.imageViewOk);
    imageViewOk.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
           String txt = et.getText().toString();
        }

    });

    mActionBar.setCustomView(mCustomView);
    mActionBar.setDisplayShowCustomEnabled(true);
}

FRAGMENT:

public class Fragment extends Fragment...{
...
@Override
public void onViewCreated(View view, Bundle savedInstanceState){

}
Arnes
  • 403
  • 1
  • 5
  • 20

1 Answers1

0

Try calling setHasOptionsMenu(true) inside your onCreate() method of the Fragment (tab) which needs to interact with the ActionBar.

Rene Juuse
  • 575
  • 8
  • 16
  • 1
    Not woking. Probably because it is CUSTOM ActionBar. – Arnes Mar 12 '15 at 17:06
  • What `Menu` you are talking about? There is no `Menu`. It is simple button on custom ActionBar – Arnes Mar 12 '15 at 18:13
  • Yes, the click is detected but can't get text from EditText. – Arnes Mar 12 '15 at 18:35
  • Are you sure the `editText` reference isn't a null reference? Do you get any kind of exceptions/errors? You could also try `editText.getText().toString()` (assuming `editText` isn't referencing a null object). – Rene Juuse Mar 12 '15 at 18:37
  • I've edited my code. editText references null. How to solve this? – Arnes Mar 12 '15 at 19:09
  • Have a look here [link](http://stackoverflow.com/questions/16574446/reference-to-edittext-in-viewpager-creates-null-pointer-exception). – Rene Juuse Mar 12 '15 at 19:22
  • It might be that the `Fragment` isn't instantiated yet and therefore the layout of `EditText` wouldn't be inflated yet which results in null reference. – Rene Juuse Mar 12 '15 at 19:35
  • I solved the problem but don't know what helped me. I just figure it out. Thanks for helping me. – Arnes Mar 12 '15 at 20:51