我正在尝试使用android开发一个Twitter客户端。我的整个代码现在没有错误,除了行#34; signIn.setOnClickListener(本);&#34 ;.我已经尝试过遵循其他建议,但他们似乎没有帮助。报告的错误是"类型View中的方法setOnClickListener(View.OnClickListener)不适用于参数(起始点)"。根据建议,似乎我应该使用" View"而不是" signIn"。可能的解释是什么,我需要在哪里纠正我的代码?
package com.HIT.bjak;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class startingpoint extends Activity implements OnClickListener {
/** developer account key for this app */
public final static String TWIT_KEY = "xxx";
/** developer secret for the app */
public final static String TWIT_SECRET = "xxx";
/** app url */
public final static String TWIT_URL = "bjak-android:///";
/** Twitter instance */
private Twitter bjak_instance;
/** request token for accessing user account */
private RequestToken bjak_RequestToken;
/** shared preferences to store user details */
private SharedPreferences Prefs;
// for error logging
private String LOG_TAG = "startingpoint";
Button signIn;
String oaVerifier=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// get the preferences for the app
bjak_instance = (Twitter) getSharedPreferences("TweetPrefs", 0);
// find out if the user preferences are set
if ( Prefs.getString("user_token", null) == null) {
// no user preferences so prompt to sign in
setContentView(R.layout.main);
// get a twitter instance for authentication
bjak_instance = new TwitterFactory().getInstance();
// pass developer key and secret
bjak_instance.setOAuthConsumer(TWIT_KEY, TWIT_SECRET);
// try to get request token
try {
// get authentication request token
bjak_RequestToken = bjak_instance.getOAuthRequestToken(TWIT_URL);
} catch (TwitterException te) {
Log.e(LOG_TAG, "TE " + te.getMessage());
}
// setup button for click listener
signIn = (Button)findViewById(R.id.signin);
signIn.setOnClickListener(this);
//attempt to retrieve access token
try
{
//try to get an access token using the returned data from the verification page
AccessToken accToken = bjak_instance.getOAuthAccessToken(bjak_RequestToken, oaVerifier);
//add the token and secret to shared prefs for future reference
Prefs.edit()
.putString("user_token", accToken.getToken())
.putString("user_secret", accToken.getTokenSecret())
.commit();
//display the timeline
setupTimeline();
}
catch (TwitterException te)
{ Log.e(LOG_TAG, "Failed to get access token: " + te.getMessage()); }
} else {
// user preferences are set - get timeline
setupTimeline();
}
}
/**
* Click listener handles sign in and tweet button presses
*/
public void onClick(View v) {
// find view
switch (v.getId()) {
// sign in button pressed
case R.id.signin:
// take user to twitter authentication web page to allow app access
// to their twitter account
String authURL = bjak_RequestToken.getAuthenticationURL();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authURL)));
break;
// other listeners here
default:
break;
}
}
/*
* onNewIntent fires when user returns from Twitter authentication Web page
*/
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//get the retrieved data
Uri twitURI = intent.getData();
//make sure the url is correct
if(twitURI!=null && twitURI.toString().startsWith(TWIT_URL))
{
//is verifcation - get the returned data
oaVerifier = twitURI.getQueryParameter("oauth_verifier");
}
}
private void setupTimeline() {
Log.v(LOG_TAG, "setting up timeline");
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}`
答案 0 :(得分:0)
这是因为您的活动实现的界面是错误的!
import android.content.DialogInterface.OnClickListener;
...
public class startingpoint extends Activity implements OnClickListener {
您应该实现此接口View.OnClickListener
。