【问题标题】:The method updateStatus(String) in the type TweetRecources is not applicable for the argument (String[])TweetRecources 类型中的方法 updateStatus(String) 不适用于参数 (String[])
【发布时间】:2015-05-25 16:12:12
【问题描述】:

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

【问题讨论】:

    标签: java twitter arguments processing


    【解决方案1】:

    正如错误消息所述,您将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));
    

    【讨论】:

    • 就是这样,你也可以像newTweet[0]一样传递数组的第一个值
    • 是的,我之前尝试过,但随后收到错误消息“只能遍历数组或 java.lang.iterable 的实例,突出显示以下代码:'for(String name: newTweet) '(格式化文本输入框的位置)。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多