-1

I have an app up in the Google Play store. I am getting crash notifications through Crashlytics/Fabric, so I don't have a true LogCat to reference. Also, I am unable to figure out how to replicate this bug on my own system.

The error I'm getting from Crashlytics is as follows:

com.company.appname.Fragment.onCreateView (Fragment.java:48)

Here's the pertinent detail for the class declaration:

import android.support.v4.app.Fragment;
public class MyTypeFragment extends Fragment {...

Here's the code from that portion of the fragment:

44 mRestartButton = (Button)contentView.findViewById(R.id.restart_button);
45 mRestartButton.setTypeface(regularTf);
46 mRestartButton.setOnClickListener(new View.OnClickListener() {
47     @Override
48     public void onClick(View v) {
49         getFragmentManager().popBackStack("startingFragment", 0);
50     }
51 });

I find it interesting that the crash report is listing the crash as being in the onCreateView() method, yet line 48 is the declaration of the onClick() method on my button's onClickListener.

I've looked into the following questions:

Both of them seem to indicate the problem being related to an incorrect id when accessing the XML for this fragment. I don't think that's the problem, however, as this is an XML portion for this fragment (and there's only one layout for this fragment):

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"

    >

    <Button
        android:id="@+id/restart_button"
        android:layout_gravity="right"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"
        android:text="Restart"

        />

One thing that might be of concern: I use this same paradigm on many of my fragments. The app's function is to drill through a series of questions to achieve an answer. The "reset" button allows them to go all the way back to the beginning without going through any of the intermediate fragments they've seen along the way. Each of those fragments has a button on it that is named with R.id.reset_button. As I said, though, I've attempted to replicate this problem on my end and cannot figure out how it occurs.

I've read some stuff that indicates this problem might be related to a fragment somehow being detached from its activity, but I've even tried leaving my app and returning, and still can't replicate this bug.

Community
  • 1
  • 1
mbm29414
  • 11,558
  • 6
  • 56
  • 87
  • ... or maybe layouts are platform specific ... with information that you provided it is not possible to answer this question ... – Selvin Jun 10 '15 at 13:34
  • @Selvin I don't understand what you mean by that comment. Can you please elaborate? I only have the one layout for each fragment. How would the platform changing matter? – mbm29414 Jun 10 '15 at 13:35
  • http://developer.android.com/guide/practices/screens_support.html#qualifiers < do you have any? (or others like layout-vXX) – Selvin Jun 10 '15 at 13:36
  • the on click listener `is inside` the onCreateView - so the reporting is fine and most probable the crash occurs here: `getFragmentManager().popBackStack("startingFragment", 0);` – Skynet Jun 10 '15 at 13:41
  • @Selvin No, like I said, there is only the one layout for all fragments. It's a fairly simple app UI-wise. – mbm29414 Jun 10 '15 at 13:43
  • @Skynet I had kind of figured that out, too (i.e., the problem is when the user taps on the "Restart" button), but I can't get it to ever crash, and I don't have enough experience to know why that line might cause a crash. – mbm29414 Jun 10 '15 at 13:44
  • are you using the appCompact v7 for this particular fragment, please post the class signature / declaration? Also [this](https://github.com/ACRA/acra) might be useful for you. – Skynet Jun 10 '15 at 13:52
  • @Skynet I added the class declaration with the associated Fragment import statement. – mbm29414 Jun 10 '15 at 19:11
  • Can someone please explain the down vote? If you have suggestions on how I can make this a better question, I'm all ears. – mbm29414 Jun 10 '15 at 19:12
  • Is this still a problem with some devices? – The Original Android Jun 13 '15 at 02:28

1 Answers1

0

I assume the code works in many devices and the bug is not easily reproducible. The only recommendation I have and it is my own coding technique is to move the posted code to onViewCreated() method instead of onCreateView(). The reason is sometimes onCreateView could not finish processing the GUI layout in time. At least, this is easy to try. Good luck in testing with different devices.

Sample code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.list_layout, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
   Context thisContext = view.getContext();

   mRestartButton = (Button) view.findViewById(R.id.restart_button);
   mRestartButton.setTypeface(regularTf);
   mRestartButton.setOnClickListener(new View.OnClickListener() {
   ...

Notes:

  • onCreateView() is changed to only returning the root view to pass onto onViewCreated()
  • onViewCreated() is now doing the UI work.
The Original Android
  • 6,147
  • 3
  • 26
  • 31
  • What are the consequences of doing this? How do I do this? Do I still associate/populate my class's UI variables in `onCreateView()`, or does it **all** get moved to `onViewCreated()`? – mbm29414 Jun 10 '15 at 19:13
  • @mbm29414, I added sample code to my post and notes. As you can see, there is little changed. – The Original Android Jun 10 '15 at 19:55
  • Thanks for the update. What would I use as the substitute for the `contentView` variable I create using the inflater in `onCreateView()`? Is that the `View view` passed in to `onViewCreated()`, or do I have to access/create it some other way? – mbm29414 Jun 10 '15 at 20:06
  • @mbm29414, Oops, that is my fault. I simply copied code from my project. Just use view.findViewById. I edited my post and hopefully others may simply copy code from the post. – The Original Android Jun 10 '15 at 20:13
  • That's what I figured, but I just wanted to make sure. So, you do this with every fragment you create? Any practical side effects or "gotchas" to be aware of? – mbm29414 Jun 10 '15 at 20:17
  • @mbm29414, Don't worry too much. One reason for onViewCreated() is to process the parameter savedInstanceState, which I normally don't do anyway. – The Original Android Jun 10 '15 at 20:19
  • `onCreateView could not finish processing the GUI layout in time` can you support this with some more detail or an official doc? – Skynet Jun 11 '15 at 03:39
  • @Skynet, I remember reading this but I could not easily find it. I will let you know if/when I find it. Besides that, it makes sense to me if the layout xml is somewhat complicated and when there are few nested layouts. – The Original Android Jun 11 '15 at 04:29
  • `onCreateView could not finish processing the GUI` would be a violation of the activity life-cycle, which is not possible under normal execution. Having said that I do get what you intend. – Skynet Jun 11 '15 at 05:44
  • @mbm29414, How is it going? Did it make a difference? – The Original Android Jun 13 '15 at 18:21
  • I haven't had a break from other work to really test this yet. Will report back when I have. – mbm29414 Jun 13 '15 at 21:08