【问题标题】:Syntax errors when declaring a function inside another function在另一个函数中声明一个函数时的语法错误
【发布时间】:2014-11-17 00:22:48
【问题描述】:

我在我的项目中使用了this answer by Jared Rummler的代码,结果如下。

public class MainActivity extends Activity {

    Button dadclink;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnButton();
    }

    public Intent addListenerOnButton() {
        dadclink = (Button) findViewById(R.id.dadclink);

        public static Intent newInstagramProfileIntent(PackageManager pm, String url) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            try {
                if (pm.getPackageInfo("com.instagram.android", 0) != null) {
                    if (url.endsWith("/")) {
                        url = url.substring(0, url.length() - 1);
                    }
                    String natgeo = url.substring(url.lastIndexOf("/") + 1);
                    intent.setData(Uri.parse("http://instagram.com/_u/" + natgeo));
                    intent.setPackage("com.instagram.android");
                    return intent;
                }
            } catch (NameNotFoundException e) {
            }
            intent.setData(Uri.parse(url));
            return intent;
        }};

但是,我在这一行遇到了语法错误:

public static Intent newInstagramProfileIntent(PackageManager pm, String url) {

出现以下错误:

  • 参数 newInstagramProfileIntent 的非法修饰符;只允许决赛
  • 标记“,”的语法错误;预计
  • 标记“)”的语法错误,;预计

如何解决这个问题?

【问题讨论】:

  • 请发布确切的错误。我的猜测是这是一个无效的演员表......
  • 如果我改变了任何东西,它会给我带来几个错误,现在代码中唯一的错误是 ; 在 dadclink = (Button) findViewById(R.id. dadclink)**;**
  • 如果这就是你的意思!
  • 不完全...错误信息是什么?如果您使用的是 eclipse,它应该会给您一条详细说明错误的消息。
  • 错误实际上在第27行:public static Intent newInstagramProfileIntent(PackageManager pm, String url) {。你不能在这样的函数中拥有一个函数。

标签: java android


【解决方案1】:

函数内不能有函数声明。

public Intent addListenerOnButton() {
    dadclink = (Button) findViewById(R.id.dadclink);

    // illegal operation
    public static Intent newInstagramProfileIntent(PackageManager pm, String url) {
        ...
    }
};

相关:Function within function in Java

如果我了解您想要做什么,您想创建一个按钮的侦听器,该按钮在单击时使用 Instagram Intent。代码应该修改成这样:

public void addListenerOnButton() {
    dadclink = (Button) findViewById(R.id.dadclink);
    dadclink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = newInstagramProfileIntent(getPackageManager(),
                    "your_string_url");

            // do something with the intent, perhaps starting an activity
            startActivity(intent);
        }
    });
}

public static Intent newInstagramProfileIntent(PackageManager pm, String url) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    try {
        if (pm.getPackageInfo("com.instagram.android", 0) != null) {
            if (url.endsWith("/")) {
                url = url.substring(0, url.length() - 1);
            }

            String natgeo = url.substring(url.lastIndexOf("/") + 1);
            intent.setData(Uri.parse("http://instagram.com/_u/" + natgeo));
            intent.setPackage("com.instagram.android");
            return intent;
        }
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    intent.setData(Uri.parse(url));
    return intent;
}

【讨论】:

  • 运行良好,清单上还需要一些工作!非常感谢:)
  • 另一个问题,如果你不介意的话.. 我怎样才能像图片一样在 Instagram 中编码例如#cat? oi60.tinypic.com/2rw8axg.jpg 在(A)中我去搜索,然后是“HASHTAGS”,然后我写“Cat”,我得到“#Cat”的结果,然后我点击“#Cat”得到所有“#Cat”图片在(B)中的“HASHTAGS”中?谢谢你:)
  • 对不起,我对此一无所知,应该作为一个新问题提出。但请不要按原样发布问题;至少展示你尝试过的努力。我们不鼓励在这里提供免费代码。
  • Andrew - 如你所知,代码运行良好,但是当我卸载 Instagram 时,它给了我一个错误,我只想让它去 play.google 安装 Instagram
  • 为此,您可能对check if the app is installed 感兴趣,然后open the Play Store programmatically。作为提示,您需要使用com.instagram.android 作为参数(取自URL of its Play Store
猜你喜欢
  • 2016-03-13
  • 2012-10-28
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 2011-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多