java - 无法在 android :onClick attribute defined on view class android. widget.ImageView 的父级或祖先上下文中找到方法

标签 java android view imageview android-view

我基本上只是在 XML 中的 android:onClick 函数中为这个特定的 ImageView 调用它:android:onClick="SendGetStartedNotification"。但因为我的函数有两个参数,所以我收到此错误方法签名不正确。它希望我删除 ParseUser 类。

public void SendGetStartedNotification(View view, ParseUser user) {

    // initiate installation query
    ParseQuery<ParseInstallation> query  = ParseInstallation.getQuery();
    query.whereEqualTo("userId", user.getUserObject());

    // send push notification
    ParsePush push = new ParsePush();
    push.setQuery(query);
    push.setMessage(ParseUser.getCurrentUser().getUsername() + " " + "thinks you should create something!");
    push.sendInBackground();

    // send notification to NotificationsActivity
    ParseObject notification = createGetStartedMessage(view, user);
    send(notification);
}

问题是我的错误消息:

12-20 22:22:37.349  29251-29251/com.app.testE/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.app.test, PID: 29251
    java.lang.IllegalStateException: Could not find method SendGetStartedNotification(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.ImageView with id 'messageGallerySave2'
            at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4485)
            at android.view.View$DeclaredOnClickListener.onClick(View.java:4449)
            at android.view.View.performClick(View.java:5204)
            at android.view.View$PerformClick.run(View.java:21153)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我有什么选择?以编程方式设置 onClick 监听器似乎也不起作用。我该怎么办?

更新:我正在使用 AsyncTask 将用户信息引入我的类(class)。

private class GetParseUserInformationTask extends AsyncTask<Void, Design, ParseUser> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            mProgressDialog = new ProgressDialog(GalleryActivity.this);
            // Set progressdialog message
            mProgressDialog.setMessage("Loading Designs...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();

        }

        @Override
        protected ParseUser doInBackground(Void... params) {

            ParseUser parseUser = null;

            // gather User information and then fetch the designs for them
            try {
                parseUser = ParseHelper.GetUserInformation(userObjectId);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            return parseUser;
        }

        @Override
        protected void onPostExecute(ParseUser user) {

            if(user == null) {
                //todo: some error message
                return;
            }

            ((TextView)findViewById(R.id.fullName)).setText(user.getName());
            ((TextView)findViewById(R.id.bio)).setText(user.getBio());
            ((TextView)findViewById(R.id.websiteLink)).setText(user.getWebsiteLink());
            ((TextView)findViewById(R.id.username)).setText(user.getUsername());
            ((TextView)findViewById(R.id.saves_number)).setText(user.getFeedDesignSaves().toString());
            ((TextView)findViewById(R.id.designs_number)).setText(String.valueOf(ParseHelper.GetUserDesignsCount(user.getUserObject())));
            ((ImageView)findViewById(R.id.profile_picture)).setImageDrawable(Drawable.createFromPath(user.getProfilePictureURL()));

            // asynchronously display the profile picture downloaded from parse
            if(user.getProfilePictureURL() != null) {
                profilePictureImageLoader.DisplayImage(user.getProfilePictureURL(), ((ImageView) findViewById(R.id.profile_picture)), null);
            }

            new GetParseUserDesignsTask().execute(user);

            mProgressDialog.dismiss();
        }
    }

最佳答案

单击按钮上的监听器。

 imageView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        // TODO Auto-generated method stub
SendGetStartedNotification(ParseUser user);

                    }
                } );

让你的方法像这样:

public void SendGetStartedNotification(ParseUser user) {

    // initiate installation query
    ParseQuery<ParseInstallation> query  = ParseInstallation.getQuery();
    query.whereEqualTo("userId", user.getUserObject());

    // send push notification
    ParsePush push = new ParsePush();
    push.setQuery(query);
    push.setMessage(ParseUser.getCurrentUser().getUsername() + " " + "thinks you should create something!");
    push.sendInBackground();

    // send notification to NotificationsActivity
    ParseObject notification = createGetStartedMessage(view, user);
    send(notification);
}

关于java - 无法在 android :onClick attribute defined on view class android. widget.ImageView 的父级或祖先上下文中找到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34390089/

相关文章:

java - fragment 实例化崩溃

c# - mvc.net 4 View 不显示 ViewBag.Message

asp.net-mvc - ASP.NET View 中的区域?

java - 如何设置 Libgdx 位图字体大小?

java - 非常基本的变量超出范围问题 - Java Android

android - View 上的通知已添加到父级?

java - Java 中的 View 可以调用 MVC 结构中的模型吗?

java - 获取异常 : The Network Adapter could not establish the connection in simple jdbc program

java - 如何在 powershell 中处理 $PSScriptRoot 路径中的空格

java - 如何使用 Selenium WebDriver java 从下拉列表中获取所有选项