我正在尝试使用this card library作为我正在尝试构建的Android应用的UI,但我遇到了一些问题。
我一直关注的作者has this guide for using the library with an existing Eclipse project,但我遇到了多个错误。
我正在Eclipse中导入选项下导入库作为“现有项目”,并将项目包含在我现有项目的构建路径中,但我不断收到有关缺失方法的错误(特别是getContext()
参数即使在导入整个库之后,也会在constructor中指定。
以下是我的代码片段:
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);
我的代码段中的每一行都出现以下错误:
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)
我知道这是一个非常具体的问题,但我希望我能在这里得到答案!
答案 0 :(得分:1)
您的错误与Cardslib无关。卡构造接受当前活动的上下文。在Android中没有getContext()函数。正确的函数是getBaseContext()。
有两种发送方式。
Card card = new Card(getBaseContext());
或
Card card = new Card(this);