-1

I am trying to add cardviews to my app from a template that it already works fine, I was understanding, learning and developping from this template and I guess I understand it at al, but obviously not.

When I try to add it to my app I get some issues with the recyclerview, I am trying to be precised

My relevant main code is:

public class MainActivity extends ActionBarActivity
        implements NavigationDrawerCallbacks {
(...)
    private List<Anime> items = new ArrayList<>();
    private RecyclerView recycler;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager lManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(mToolbar);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.fragment_drawer);

    // Set up the drawer.
    mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
    coordinates = new String[3];
    coordinates[0]="";coordinates[1]="";coordinates[2]="";
    location = null;
    textViewTitulo = (TextView) findViewById(R.id.textViewCoordinates);

    //this line is the SOLUTION 
    recicle();


}

(...)

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        (...)
        items.add(new Anime(R.drawable.iamhere, "You are at:", "lat. 4.59 long. 67.9 alt. 0.0"));
            recicle();
     }
    public void recicle() {
    //Obtaining the recicler
    recicler = (RecyclerView) findViewById(R.id.reciclador);
        recicler.setHasFixedSize(true);

        // Usar un administrador para LinearLayout
        lManager = new LinearLayoutManager(this);
        recycler.setLayoutManager(lManager);

        // Crear un nuevo adaptador
        adapter = new AnimeAdapter(items);
        recycler.setAdapter(adapter);
    }

The class Anime just contains an int and two string as atruibutes

It compiles ok, but I think the issue is in the layout. I have a navigation drawer fragment, and I am almost sure that my problem is managing the recyclerView in the fragment in the XML. This is all my main XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include android:id="@+id/toolbar_actionbar" layout="@layout/toolbar_default"
        android:layout_width="match_parent" android:layout_height="wrap_content" />

    <android.support.v4.widget.DrawerLayout android:id="@+id/drawer"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:layout_below="@+id/toolbar_actionbar">

        <FrameLayout android:id="@+id/container" android:layout_width="match_parent"
            android:clickable="true" android:layout_height="match_parent" >

      <!---This is the RecyclerView at hand -->
            <android.support.v7.widget.RecyclerView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/reciclador"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="3dp"
                android:scrollbars="vertical" />

        </FrameLayout>

        <!-- android:layout_marginTop="?android:attr/actionBarSize"-->
        <fragment android:id="@+id/fragment_drawer"
            android:name="com.example.jesus.myapplication.NavigationDrawerFragment"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent" android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer" />


    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

I do not think the issue is in the inflate or something related to this because of my other app (the one that comes from the template) works perfectly.

If someone wants to get this template, is here in the basic version. I did not add the reference of this because I have already change the code. But this is from here (spanish).

The Logcat returns me this:

07-23 13:52:10.962  21206-21206/com.example.jesus.myapplication E/RecyclerView? No adapter attached; skipping layout
07-23 13:52:11.006  21206-21206/com.example.jesus.myapplication D/AndroidRuntime? Shutting down VM
07-23 13:52:11.010  21206-21206/com.example.jesus.myapplication E/AndroidRuntime? FATAL EXCEPTION: main
    Process: com.example.jesus.myapplication, PID: 21206
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference
            at android.support.v7.widget.RecyclerView.computeVerticalScrollRange(RecyclerView.java:1535)
            at android.view.View.onDrawScrollBars(View.java:12943)
            at android.view.View.draw(View.java:15237)
            at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:2997)
            at android.view.View.updateDisplayListIfDirty(View.java:14167)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.updateDisplayListIfDirty(View.java:14162)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1089)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.updateDisplayListIfDirty(View.java:14162)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.updateDisplayListIfDirty(View.java:14162)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.updateDisplayListIfDirty(View.java:14162)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.updateDisplayListIfDirty(View.java:14162)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.updateDisplayListIfDirty(View.java:14162)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.updateDisplayListIfDirty(View.java:14162)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.draw(View.java:15234)
            at android.widget.FrameLayout.draw(FrameLayout.java:598)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2650)
            at android.view.View.updateDisplayListIfDirty(View.java:14167)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
            at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
            at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
            at android.view.ViewRootImpl.draw(ViewRootImpl.java:2530)
            at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2352)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler

Then, we can see the RecyclerView is guilty as I presumed. It explains me the RecyclerView points to a null pointer and then get a null pointer exception but, sincerely, I have no idea how can I resolve it.

Could you help me trying to find my error? It compiles ok, when I try to run it in my device it returns me an FC just in the openning.

txitxo0
  • 21
  • 4
  • You missed `setContentView(R.layout.yourlayout);` in `onCreate(....)` – M D Jul 23 '15 at 11:06
  • No sorry, Maybe I have brief too much. I am going to edit my main code. Thank you – txitxo0 Jul 23 '15 at 11:12
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Jul 23 '15 at 11:17
  • Okay! I'll keep in touch – txitxo0 Jul 23 '15 at 11:19
  • The Logcat returns me this: 07-23 13:52:11.010 21206-21206/com.example.jesus.myapplication E/AndroidRuntime? FATAL EXCEPTION: main Process: com.example.jesus.myapplication, PID: 21206 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference Then, we can see the RecyclerView is guilty as I presume. It explains me the RecyclerView points to a null pointer and the get a null pointer exception but, sincerely, I have no idea how can I resolve it. I've updated the OP – txitxo0 Jul 23 '15 at 15:21

1 Answers1

1

I have just realized what it was wrong. It has not got anything related to both the recyclerview or layout in particular. I just didn't initialize the variables: LinearLayoutManager and my adapter (AnimeAdapter).

Just adding recicle(); to the oncreate method works

txitxo0
  • 21
  • 4