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.
public class ADatabase extends AppCompatActivity implements
OnFragmentInteractionListener {
*// fragments insive Activity*
final FDbTables fDbTables = FDbTables.newInstance();
final FDbQuery fDbQuery = FDbQuery.newInstance();
final FDbStoredQueries fDbStoredQuries = FDbStoredQueries.newInstance();
/**
* 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;
public FloatingActionButton getFabRun() {
return fabRun;
}
@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();
tvDbHeader = (TextView) findViewById(R.id.tvDbHeader);
tvDbTablesCnt = (TextView) findViewById(R.id.tvDbTablesCnt);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabanim_tabs);
tabLayout.setupWithViewPager(mViewPager);
fabRun = (FloatingActionButton) findViewById(R.id.fabRun);
fabRun.setEnabled(false);
...
}
public void run(final View view) {
final String sql = fDbQuery.getEtSql().getText().toString(); // <-- here
NPE. because fDbQuery.getEtSql().getText() sometimes is NULL. WHY???
...
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
if (position == 0) return fDbTables;
if (position == 1) return fDbQuery;
if (position == 2) return fDbStoredQuries;
throw new IllegalArgumentException("Must be only three fragments. But
position=" + position + ". Check fragments.");
}
@Override
public int getCount() { return 3; }
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return getString(R.string.Tables);
case 1: return getString(R.string.SQL_query);
case 2: return getString(R.string.Saved_queries);
}
return null;
}
}
}
public class FDbQuery extends Fragment {
public static FDbQuery newInstance() {
FDbQuery fragment = new FDbQuery();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
private EditText etSql; // I need text from this View
public EditText getEtSql() {
return etSql;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fdb_query, container, false);
return view;
}
@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);
}
}
I can not understand. Why some users have the crash? What is wrong?
--
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/81e4333f-b348-4517-8ebd-ee4ddb875e7e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.