On Wed, Feb 24, 2016 at 2:38 AM, Evgeniy Egorov <[email protected]>
wrote:

> Help! No idea how to fix crash. StackOverflow didnt help me. I can not
> reproduce this crash on emulator or my smartphone but 10% of users have
> this crash every day regularly. I use Crashelitics.
>
> I have:
> Activity contains ViewPager with three Fragments. Activity contains a
> button FloatingActionButton. Fragment contains EditText. User tap on button
> from Activity. Inside onClick of this FloatingActionButton I try to get
> text from EditText which inside Fragment. Then I have NullPointerExaption
> because of returned EditText is NULL.
>

When you have a view pager with fragments, by default one extra fragment is
retained in memory "offscreen" so it can be paged to quickly. If you're on
the first page, the 3rd fragment does not exist. You have a reference to it
because you created it, but it would have been destroyed and its views
cleared out. If then you click a button on the activity and attempt to
access that third fragment, you're going to have a bad day.

If you explain what you're attempting to do, we could provide suggestions
on fixing your issue.

Some general unsolicited comments on your code:

    *// fragments insive Activity*
>     final FDbTables fDbTables = FDbTables.newInstance();
>     final FDbQuery fDbQuery = FDbQuery.newInstance();
>     final FDbStoredQueries fDbStoredQuries = FDbStoredQueries.newInstance();
>
>
If these are the fragments being displayed, they should be "owned" by the
adapter providing them, not the activity itself.


>
>     /**
>      * The {@link ViewPager} that will host the section contents.
>      */
>     ViewPager mViewPager;
>     // three buttons
>     private FloatingActionButton fabTFS;
>
>     private FloatingActionButton fabRun; // <-- user tap this button
>     private FloatingActionButton fabRefreshDB;
>
>
I don't know what your app looks like, but having 3 Fabs on an Activity
raises an eyebrow.


> public FloatingActionButton getFabRun() {
>     return fabRun;
> }
>
>
Activities rarely should expose UI elements publicly.


>  @Override
>
> protected void onCreate(Bundle savedInstanceState) {
>     super.onCreate(savedInstanceState);
>     setContentView(R.layout.activity_database);
>
>     FragmentTransaction transaction =  
> getSupportFragmentManager().beginTransaction();
>     // Create the adapter that will return a fragment for each of the three
>     // primary sections of the activity.
>     mSectionsPagerAdapter = new 
> SectionsPagerAdapter(getSupportFragmentManager());
>
>     // Set up the ViewPager with the sections adapter.
>     mViewPager = (ViewPager) findViewById(R.id.container);
>     mViewPager.setAdapter(mSectionsPagerAdapter);
>     // Commit the transaction
>     transaction.commit();
>
>
This fragment transaction is pointless.


>     public EditText getEtSql() {
>
>         return etSql;
>
>     }
>
>
Again, publicly exposing UI elements is generally a bad idea.


>     @Override
>
>     public void onResume() { // here could be onCreateView or onViewCreated 
> but useless
>         super.onResume();
>
>         etSql = (EditText) getView().findViewById(R.id.etSql);
>
>         ((ADatabase) getActivity()).getFabRun().setEnabled(true);
>
>     }
>
> }
>
>
1, you should not be getting variables in onResume - get them in
onCreateView, or onViewCreated.
2, you should not be accessing the Activity from the fragment to tell it to
change its UI. This adds a dependency on that specific Activity to this
fragment, making it so you can't reuse the fragment in other activities.

-------------------------------------------------------------------------------------------------
TreKing <http://sites.google.com/site/rezmobileapps/treking> - Chicago
transit tracking app for Android-powered devices

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/CANCScghLvC5gz0Z7TKiapjuAmdiXeDGok%2B%3DRx82UBqLtnVWqaQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to