簡體   English   中英

在視圖類android.widget.ImageView上定義的android:onClick屬性的父級或祖先上下文中找不到方法

[英]Could not find method in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.ImageView

我基本上只是在XML中針對特定ImageView的android:onClick函數中調用此函數: android:onClick="SendGetStartedNotification" 但是因為我的函數有兩個參數,所以我得到了這個錯誤, Method has incorrect signature 它希望我刪除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將用戶信息帶入我的班級。

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM