【问题标题】:Trouble using cardslib with existing Eclipse Android project在现有的 Eclipse Android 项目中使用 cardlib 时遇到问题
【发布时间】:2014-01-19 09:35:28
【问题描述】:

我正在尝试将 this card library 用于我正在尝试构建的 Android 应用程序的 UI,但我遇到了一些问题。

作者has this guide for using the library with an existing Eclipse project我已经完全关注了,但我遇到了多个错误。

我在 Eclipse 的 Import 选项下将库作为“现有项目”导入,并将该项目包括在我现有项目的构建路径中,但我不断收到有关缺少方法的错误(特别是 getContext() 中指定的参数constructor),即使在导入整个库之后。

这是我的代码的 sn-p:

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);

我的代码 sn-p 中的每一行都出现以下错误:

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)

我知道这是一个非常具体的问题,但我希望我能在这里得到答案!

【问题讨论】:

  • "我遇到多个错误" ,发布这些错误。
  • 对不起,我已经修改了我的帖子!
  • 您在哪种方法中使用 Card card = new Card(getContext());?

标签: java android eclipse user-interface cardslib


【解决方案1】:

您的错误与 Cardslib 无关。卡片构造接受您当前活动的上下文。 Android 中没有 getContext() 函数。正确的函数是 getBaseContext()。

有两种发送方式。

Card card = new Card(getBaseContext());

Card card = new Card(this);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    相关资源
    最近更新 更多