我正在尝试定义一个自定义Java类(扩展LinearLayout),该类需要在单击时启动 Activity 。我的代码如下所示:

public ArizaSatiri(Context context/*, AttributeSet attrs , final Activity aktivite*/ , JSONObject mysql_satiri)
    {
        super(context/*, attrs*/);

        // code to initialize my view :

        final Context finalContext = context;

        this.setOnClickListener(new OnClickListener() {@Override
            public void onClick(View v) {
                Intent newIntent = new Intent(finalContext, ArizaDetaylari.class);
                finalContext.startActivity(newIntent);
            }//onClick
        });


    }

但是,当我单击实例化 View 时,出现错误:
Calling startActivity() from outside of an Activity  context requires the
FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

从代码中可以看到,我尝试将Activity传递给构造函数,并且成功了。但这是正确的方法吗?您会推荐哪种方式?

编辑:

而且我还需要在某个时候调用setTypeFace()。我应该使用上下文还是 Activity ?

最佳答案

试试这个:

this.setOnClickListener(new OnClickListener() {@Override
        public void onClick(View v) {
            Intent newIntent = new Intent(finalContext, ArizaDetaylari.class);
            newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            finalContext.startActivity(newIntent);
        }//onClick
    });

10-02 05:45