> > I cannot divulge the source code as it is for work - however I've done >>> some renaming and stripped it down. However, after stripping it down to >>> just the parts which I suspect cause the bug, and enabling the line that >>> causes the bug, the bug doesn't happen anymore! >> >> >>> So this source code is just to give a clue to anyone else who >>> experiences this, as to where to look. This code doesn't actually trigger >>> the bug. >> >> >>> onCreate calls showSingleUse, which spawns a SingleUseFragment, which >>> calls onSingleUse in the activity (because it implements an interface >>> called OnFragmentUtilityListener). onSingleUse then calls an AsyncTask >>> which does a findViewById in its onPreExecute. >> >> >>> Disabling the findViewById, and moving it to onCreate, solves the >>> problem. >> >> >>> *SwiperActivity.java:* >> >> >>> import android.os.AsyncTask; >> >> import android.os.Build; >> >> import android.os.Bundle; >> >> import android.support.v4.app.FragmentActivity; >> >> import android.widget.TextView; >> >> >>> import com.actionbarsherlock.app.SherlockFragmentActivity; >> >> >>> public class SwiperActivity extends SherlockFragmentActivity implements >>> OnFragmentUtilityListener { >> >> * TextView textviewLastUpdated; // this is how the NPE was fixed* >> >> ReloadTask reloadTask; >> >> @Override >> >> protected void onCreate(Bundle savedInstanceState) { >> >> super.onCreate(savedInstanceState); >> >> setContentView(R.layout.main); >> >> * textviewLastUpdated = (TextView) >>> findViewById(R.id.textviewLastUpdated); // this is how the NPE was fixed >>> * >> >> showSingleUse(this); >> >> } >> >> public static void showSingleUse(FragmentActivity activity) { >> >> try { >> >> if >>> (activity.getSupportFragmentManager().findFragmentByTag("singleusefragment") >>> >>> == null) { >> >> activity.getSupportFragmentManager().beginTransaction().add(new >>> SingleUseFragment(), "singleusefragment").commit(); >> >> } >> >> } catch (IllegalStateException e) {} >> >> } >> >> @Override >> >> public void onSingleUse() { >> >> startReloadTask(); >> >> } >> >> private class ReloadTask extends AsyncTask<Void, Void, Void> { >> >> @Override >> >> protected void onPreExecute() { >> >> showLoading(); >> >> } >> >> @Override >> >> protected Void doInBackground(Void... arg0) { >> >> return null; >> >> } >> >> @Override >> >> protected void onPostExecute(Void result) { >> >> hideLoading(); >> >> } >> >> } >> >> public static boolean allowsExecutorChange() { >> >> return Build.VERSION.SDK_INT > 10; >> >> } >> >> void startReloadTask() { >> >> reloadTask = new ReloadTask(); >> >> if (allowsExecutorChange()) { >> >> reloadTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); >> >> } else { >> >> reloadTask.execute(); >> >> } >> >> } >> >> void showLoading() { >> >> // this was causing the NPE! >> >> *//TextView textviewLastUpdated = (TextView) >>> findViewById(R.id.textviewLastUpdated);* >> >> if (textviewLastUpdated!=null) textviewLastUpdated.setText("Loading..."); >> >> } >> >> void hideLoading() { >> >> // this was causing the NPE! >> >> *//TextView textviewLastUpdated = (TextView) >>> findViewById(R.id.textviewLastUpdated);* >> >> if (textviewLastUpdated!=null) textviewLastUpdated.setText("Loaded!"); >> >> } >> >> @Override >> >> public void onDismissedDialog(String tag, Object data) {} >> >> @Override >> >> public void onClickedPositiveDialog(String tag, Object data) {} >> >> } >> >> >>> * >>> * >> >> *OnFragmentUtilityListener.java:* >> >> >>> public interface OnFragmentUtilityListener { >> >> public void onDismissedDialog(String tag, Object data); >> >> public void onClickedPositiveDialog(String tag, Object data); >> >> public void onSingleUse(); >> >> } >> >> >>> * >>> * >> >> *SingleUseFragment.java:* >> >> >>> /** hack-ish way to detect rotation and not do it again */ >> >> public class SingleUseFragment extends Fragment { >> >> boolean wasRotating = false; >> >> @Override >> >> public void onActivityCreated(Bundle state) { >> >> super.onActivityCreated(state); >> >> setRetainInstance(true); >> >> } >> >> static SingleUseFragment newInstance() { >> >> return new SingleUseFragment(); >> >> } >> >> @Override >> >> public void onAttach(Activity activity) { >> >> super.onAttach(activity); >> >> if (!wasRotating) ((OnFragmentUtilityListener) activity).onSingleUse(); >> >> } >> >> @Override >> >> public void onSaveInstanceState(Bundle outState) { >> >> super.onSaveInstanceState(outState); >> >> wasRotating = true; >> >> } >> >> } >> >>
-- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en