TweetRecources 类型中的方法 updateStatus(String) 不适用于参数 (String[])

The method updateStatus(String) in the type TweetRecources is not applicable for the argument (String[])

我目前正在使用处理来创建一个界面,允许用户将文本输入文本框(使用 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()); 
    }
}

如错误消息所述,您将 String 数组 作为参数传递给接受单个 String.[=20 的方法=]

你在哪里:

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

updateStatus 方法接受单个字符串作为参数并且您正在传递字符串数组。

您可以通过多种方式解决此问题:

Status status = twitter.updateStatus(newTweet[0]);//if you just want first element from array

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