我目前正在使用处理来创建一个界面,允许用户在文本框中输入文本(使用controlP5库),然后将其发送到twitter。但是以下错误消息不断显示:TweetRecources类型中的方法updateStatus(String)不适用于参数(String [])。有关如何解决这个问题的任何想法? 代码:
import controlP5.*;
PImage twitterBird;
// import twitter4j library
import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
// use the List and Date class when dealing with tweets
import java.util.*;
Twitter twitter; // create instance of Twitter object
ControlP5 cp5;
String[] newTweet = {"Please Enter Tweet"};
void setup() {
background(255);
size(900,600);
// Authentication on Twitter
// see information sheet "Twitter Apps" to find out how to get these details
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("*Customer key*");
cb.setOAuthConsumerSecret("*Consumer Secret*");
cb.setOAuthAccessToken("Access Token");
cb.setOAuthAccessTokenSecret("Token Secret");
// create TwitterFactory object and pass the configuration
TwitterFactory tf = new TwitterFactory(cb.build());
// Initialise Twitter object by retrieving instance from the TwitterFactory
twitter = tf.getInstance();
twitterBird = loadImage("twitterBird.jpg");
PFont font = createFont("arial",25);
cp5 = new ControlP5(this);
int y = 260;
int spacing = 90;
for(String name: newTweet){
cp5.addTextfield(name)
.setPosition(200,y)
.setSize(500,60)
.setColorBackground(0xffffffff)
.setFont(font)
.setFocus(true)
.setColor(color(10,10,255))
;
y += spacing;
}
textFont(font);
}
void draw() {
image(twitterBird, 150, 0, 615, 292);
if(keyPressed==true){
if(key == '\n'){
tweet();
}
}
}
void tweet()
{
// try to send tweet
try
{
Status status = twitter.updateStatus(newTweet);
System.out.println("Status updated to [" + status.getText() + "].");
}
// tell us if try fails
catch (TwitterException te)
{
System.out.println("Error: "+ te.getMessage());
}
}
答案 0 :(得分:0)
正如错误消息所示,您将String
的数组作为参数传递给接受单String
的方法。
你在哪里:
String[] newTweet = {"Please Enter Tweet"};
由于这是一个只有一个元素的数组,您可能需要:
String newTweet = "Please Enter Tweet";
请注意,由于newTweet
无论如何只有一个元素,因此不需要以下循环:
for(String name: newTweet) { ... }
因此,您应移除for
,并将newTweet
作为参数传递给addTextfield()
:
cp5.addTextfield(newTweet)
.setPosition(200,y)
.setSize(500,60)
.setColorBackground(0xffffffff)
.setFont(font)
.setFocus(true)
.setColor(color(10,10,255));
答案 1 :(得分:0)
updateStatus
方法接受单个String作为参数,并且您正在传递字符串数组。
您可以通过多种方式解决此问题:
Status status = twitter.updateStatus(newTweet[0]);//if you just want first element from array
OR
String newTweet = "Please Enter Tweet";//you just have one element, so dont use array
或者
for (String tweet : newTweet) {//if you need array and all the elements of an array
Status status = twitter.updateStatus(tweet);
...
}