-4

我是Android的新手。我只知道android中所有方法的onCreate方法。这一天我刚开始学习Android。

搜索词'PART Z'!并搜索单词“END OF PART Z”。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    //PART Z
    String consumerKey = "YII";
    String consumerSecret = "YII";
    String accessToken = "YII";
    String accessTokenSecret = "YII";   

    //Instantiate a re-usable and thread-safe factory
    TwitterFactory twitterFactory = new TwitterFactory();

    //Instantiate a new Twitter instance
    Twitter twitter = twitterFactory.getInstance();

    //setup OAuth Consumer Credentials
    twitter.setOAuthConsumer(consumerKey, consumerSecret);

    //setup OAuth Access Token
    twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret));

     try {
         User user = twitter.verifyCredentials();
         List<Status> statuses = twitter.getMentionsTimeline();
         System.out.println("Showing @" + user.getScreenName() + "'s mentions.");
         for (Status status : statuses) {
             System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
         }
     } catch (TwitterException te) {
         te.printStackTrace();
         System.out.println("Failed to get timeline: " + te.getMessage());
     }
    int a = 6;
    int b = 2; 
    System.out.println(a+b);
    //END OF PART Z
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

似乎 Z 部分中的所有代码都不起作用。我把 Z 部分的所有代码都写错了吗?我必须将这些代码放在什么方法中?你能帮我解决我的问题吗?

4

1 回答 1

1

试试这段代码,我只在创建函数中将 z 的部分结尾之后的位置更改为第一个和第二个可达语句。

@Override
protected void onCreate(Bundle savedInstanceState) {

 //END OF PART Z
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
//PART Z
    String consumerKey = "YII";
    String consumerSecret = "YII";
    String accessToken = "YII";
    String accessTokenSecret = "YII";   

    //Instantiate a re-usable and thread-safe factory
    TwitterFactory twitterFactory = new TwitterFactory();

    //Instantiate a new Twitter instance
    Twitter twitter = twitterFactory.getInstance();

    //setup OAuth Consumer Credentials
    twitter.setOAuthConsumer(consumerKey, consumerSecret);

    //setup OAuth Access Token
    twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret));

     try {
         User user = twitter.verifyCredentials();
         List<Status> statuses = twitter.getMentionsTimeline();
         System.out.println("Showing @" + user.getScreenName() + "'s mentions.");
         for (Status status : statuses) {
             System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
         }
     } catch (TwitterException te) {
         te.printStackTrace();
         System.out.println("Failed to get timeline: " + te.getMessage());
     }
    int a = 6;
    int b = 2; 
    System.out.println(a+b);

}
于 2013-07-31T04:38:33.433 回答