gabrielemariotti / cardslib

Android Library to build a UI Card
4.67k stars 1.19k forks source link

Help: Error inflating class fragment #535

Closed DuarteBarbosaPT closed 8 years ago

DuarteBarbosaPT commented 8 years ago

Hi I am pretty new to androi studio and i got this error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.quarkprojects.socialnetworkcards/com.quarkprojects.socialnetworkcards.MainActivity}: android.view.InflateException: Binary XML file line #13: Binary XML file line #13: Error inflating class fragment
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                          at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                          at android.os.Looper.loop(Looper.java:148)

Total error at: http://pastebin.com/va7DG1tf

This is my fragment code: http://pastebin.com/pgRi9rLq

This is my main_activity.xml: http://pastebin.com/nm3d9Mn2

This is my fragment_cards_activity.xml http://pastebin.com/bP9wi7gF

Thanks

// UPDATE: I have changed alot of the code to something near what you have here: https://github.com/gabrielemariotti/cardslib/blob/d80384806cdffd2acc559bf1167251dcc797fa97/demo/stock/src/main/java/it/gmariotti/cardslib/demo/fragment/v1/CardFragment.java

And it crashed again. Unable to start activity ComponentInfo{com.quarkprojects.socialnetworkcards/com.quarkprojects.socialnetworkcards.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void it.gmariotti.cardslib.library.view.CardView.setCard(it.gmariotti.cardslib.library.internal.Card)' on a null object reference

it allways crashed after this: cardView.setCard(card); And on debugging, this comes always null:

CardView cardView = (CardView) getActivity().findViewById(R.id.carddemo_card_id);

Update 2: Fixed it by creating a onAcitivityCreated

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initCard();
    }