无法通过活动上下文

时间:2013-08-30 13:56:17

标签: android android-context

您好我正在尝试使用其中一个库中的对象,但我无法通过我的相关上下文。

构造函数是:

public AmbilWarnaDialog(final Context context, int color, OnAmbilWarnaListener listener)

在我的课堂上,我将它用于构造函数:

AmbilWarnaDialog dialog = AmbilWarnaDialog(this, initialColor, new OnAmbilWarnaListener()
                     {

                public void onOk(AmbilWarnaDialog dialog, int[] color) {
                        // color is the color selected by the user.
                }

                public void onCancel(AmbilWarnaDialog dialog) {
                        // cancel was selected by the user
                }
            });

我收到此错误:

The method AmbilWarnaDialog(Settings, int, new AmbilWarnaDialog.OnAmbilWarnaListener(){}) is undefined for the type Settings

我也尝试过getApplicationContex()和Settings.this,但它无效。

我的进口商品是:

import yuku.ambilwarna.AmbilWarnaDialog;
import yuku.ambilwarna.AmbilWarnaKotak;
import yuku.ambilwarna.AmbilWarnaDialog.OnAmbilWarnaListener;

3 个答案:

答案 0 :(得分:1)

试试这个:

Context context;

private methodName(Context context) {
    this.context = context;
}

在Activity中调用方法:

    methodName(this);

答案 1 :(得分:1)

你无法直接调用构造函数,实例化一个类使用new关键字,如下所示:

AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, initialColor, new OnAmbilWarnaListener() { ...

答案 2 :(得分:0)

您正在Settings类中的某处调用此方法(我假设Settings不会扩展Context)。你可能会做这样的事情:

public class MyClass extends Context {

public myMethod(){

    new Settings(){

// the keyword this references to the Settings object instance, not MyClass object instance 
// to reference Context use MyClass.this
// like Henry says below, make sure you use the new keyword to initialize objects
AmbilWarnaDialog dialog = new AmbilWarnaDialog(MyClass.this, initialColor, new OnAmbilWarnaListener() {...}

       }
    }

}