创建一个新的方法意图 [英] Creating an intent in a new method

查看:91
本文介绍了创建一个新的方法意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想的意图,开始一个活动,只是会弹出一个对话框弹出框,告诉用户如何使用该应用程序。

So I want an intent to start an Activity that simply brings up a dialog popup box telling the user how to use the app.

我有code:

private final View.OnClickListener btnClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.about_box:
            Intent i = new Intent(this, About.class);
            startActivity(i);
            break;
        }
    }
}

但目的是给我的错误:

but the Intent is giving me the error:

构造意图(新View.OnClickListener(){},类)是   未定义

The constructor Intent(new View.OnClickListener(){}, Class) is undefined

这是解决方法你知道吗?

Any idea on workarounds?

感谢。

推荐答案

现在的问题是,你是另一类有内部和逝去的意图错误的情况下。你必须通过它的权利范围内。看看下面的例子。

The problem is that you are inside another class there and are passing the Intent a wrong context. You have to pass it the right context. Take a look at the example below.

// your Activity

public class MyActivity extends Activity {

    Context ctx = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ctx = getApplication();
    }

    private final View.OnClickListener btnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.about_box:

                Intent i = new Intent(ctx, About.class);
                startActivity(i);
                break;
            }
        }
    }

这篇关于创建一个新的方法意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆