Trouble using cardslib with existing Eclipse Android project

1.2k views Asked by At

I'm trying to use this card library for the UI of the Android app I'm trying to build, but I'm running into some problems.

The author has this guide for using the library with an existing Eclipse project which I've followed throughly, but I'm getting multiple errors.

I'm importing the library as an "Existing project" under the Import options in Eclipse, and including the project in my existing project's build path, but I keep getting errors regarding missing methods (specifically the getContext() argument specified in the constructor), even after importing the entire library.

Here is a snippet of my code:

import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardHeader;

public class MainActivity extends Activity {

    Card card = new Card(getContext());

    CardHeader header = new CardHeader(getContext());

    card.addCardHeader(header);

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

    cardView.setCard(card);

I get the following errors for each respective line in my code snippet:

The method getContext() is undefined for the type MainActivity

The method getContext() is undefined for the type MainActivity

Multiple markers at this line
    - Syntax error on token(s), misplaced construct(s)
    - Syntax error on token "header", VariableDeclaratorId expected after 

Multiple markers at this line
    - CardView cannot be resolved to a type
    - CardView cannot be resolved to a type
    - The method getActivity() is undefined for the type 
     MainActivity

Multiple markers at this line
    - Syntax error on token "card", VariableDeclaratorId expected after 
     this token
    - Syntax error on token(s), misplaced construct(s)

I know this is a very specific question, but I'm hoping I can get an answer here!

1

There are 1 answers

0
Sharjeel On

Your error has nothing to do with Cardslib. Card construction accepts Context of your current Activity. There's no function as getContext() in Android. The correct function is getBaseContext().

There are two ways to send it.

Card card = new Card(getBaseContext());

or

Card card = new Card(this);