gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:41:24 25 4
gpt4 key购买 nike

我基本上只是在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com