4

私の MainActivity はアクティビティを拡張getContext()し、MainActivity のインスタンスが渡された別のクラスで使用し、そこで正常に動作します、私のコード:

convertView = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.nowview, parent, false);

クラス コンストラクター:

public PictureCard(String url, String title, int index,
                                             int pos, MainActivity parent)
{
    this.parent = parent;
    // ...
}

クラスの呼び方

Card newCard = new PictureCard(links.get(index) , titles.get(index),
                                                  index, position, parent);

(親は MainActivity クラスから this として渡されます)

4

3 に答える 3

4

getApplicationContext()の代わりに使用してみましたgetContext()か?

これらのリンクが役立つ場合があります。

http://developer.android.com/reference/android/app/Activity.html

getApplication() と getApplicationContext() の比較

getContext() 、 getApplicationContext() 、 getBaseContext() と「this」の違い

于 2013-05-18T05:49:50.357 に答える
0

MainActivity を渡す代わりに、このようなパラメーターとしてコンテキストを渡すことができます

public PictureCard(String url, String title, int index, int pos, Context parent)

のように呼び出します

Card newCard = new PictureCard(links.get(index) , titles.get(index), index, position, MainActivity.this);
于 2013-05-18T05:53:39.383 に答える
0

MainActivtyextendsの場合は、コンテキストとしてActivty使用します。parent

convertView = LayoutInflater.from(parent)
                        .inflate(R.layout.nowview, parent, false);

また、ここで間違ったインスタンスを渡していると思われます:

Card newCard = new PictureCard(links.get(index) , titles.get(index),
                                              index, position, parent);
                                                               ^^^^^^

である必要がありますthis。またはMainActivty.this、内部クラス内にある場合。

于 2013-05-18T05:53:40.463 に答える