这是主要活动的代码
package pidevelopers.floata;
import org.json.JSONObject;
import org.json.JSONTokener;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.facebook.android.SessionStore;
public class MainActivity extends Activity {
Facebook mFacebook;
CheckBox mFacebookBtn;
ProgressDialog mProgress;
ToggleButton start;
Button twitterbutton;
String[] PERMISSIONS = new String[] { "publish_actions" };
String APP_ID = "231620133714944";
int isstart;
static Twitter twitter;
protected static final String AUTHENTICATION_URL_KEY = "AUTHENTICATION_URL_KEY";
protected static final int LOGIN_TO_TWITTER_REQUEST = 0;
static SharedPreferences mSharedPreferences;
static final String PREFERENCE_NAME = "twitter_oauth";
static final String ACCESS_TOKEN = "oauth_token";
static final String ACCESS_SECRET = "oauth_token_secret";
AccessToken accessToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFacebookBtn = (CheckBox) findViewById(R.id.cb_facebook);
start = (ToggleButton) findViewById(R.id.start);
twitterbutton = (Button) findViewById(R.id.twitter);
mProgress = new ProgressDialog(this);
mFacebook = new Facebook(APP_ID);
SessionStore.restore(mFacebook, this);
twitterbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
loginToTwitter();
String token = "" + accessToken;
Toast.makeText(getApplicationContext(), token, 500).show();
}
});
if (mFacebook.isSessionValid()) {
mFacebookBtn.setChecked(true);
String name = SessionStore.getName(this);
name = (name.equals("")) ? "Unknown" : name;
mFacebookBtn.setText(" Facebook (" + name + ")");
}
mFacebookBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onFacebookClick();
}
});
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (isstart == 1) {
stopService(new Intent(MainActivity.this, Chathead.class));
isstart = 2;
} else {
startService(new Intent(MainActivity.this, Chathead.class));
isstart = 1;
}
}
});
}
private void onFacebookClick() {
if (mFacebook.isSessionValid()) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Delete current Facebook connection?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
fbLogout();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
mFacebookBtn.setChecked(true);
}
});
final AlertDialog alert = builder.create();
alert.show();
} else {
mFacebookBtn.setChecked(false);
mFacebook.authorize(this, PERMISSIONS, -1,
new FbLoginDialogListener());
}
}
private final class FbLoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
SessionStore.save(mFacebook, MainActivity.this);
mFacebookBtn.setText(" Facebook (No Name)");
mFacebookBtn.setChecked(true);
getFbName();
}
public void onFacebookError(FacebookError error) {
Toast.makeText(MainActivity.this, "Facebook connection failed",
Toast.LENGTH_SHORT).show();
mFacebookBtn.setChecked(false);
}
public void onError(DialogError error) {
Toast.makeText(MainActivity.this, "Facebook connection failed",
Toast.LENGTH_SHORT).show();
mFacebookBtn.setChecked(false);
}
public void onCancel() {
mFacebookBtn.setChecked(false);
}
}
private void getFbName() {
mProgress.setMessage("Finalizing ...");
mProgress.show();
new Thread() {
@Override
public void run() {
String name = "";
int what = 1;
try {
String me = mFacebook.request("me");
JSONObject jsonObj = (JSONObject) new JSONTokener(me)
.nextValue();
name = jsonObj.getString("name");
what = 0;
} catch (Exception ex) {
ex.printStackTrace();
}
mFbHandler.sendMessage(mFbHandler.obtainMessage(what, name));
}
}.start();
}
private void fbLogout() {
mProgress.setMessage("Disconnecting from Facebook");
mProgress.show();
new Thread() {
@Override
public void run() {
SessionStore.clear(MainActivity.this);
int what = 1;
try {
mFacebook.logout(MainActivity.this);
what = 0;
} catch (Exception ex) {
ex.printStackTrace();
}
mHandler.sendMessage(mHandler.obtainMessage(what));
}
}.start();
}
private Handler mFbHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
mProgress.dismiss();
if (msg.what == 0) {
String username = (String) msg.obj;
username = (username.equals("")) ? "No Name" : username;
SessionStore.saveName(username, MainActivity.this);
mFacebookBtn.setText(" Facebook (" + username + ")");
Toast.makeText(MainActivity.this,
"Connected to Facebook as " + username,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Connected to Facebook",
Toast.LENGTH_SHORT).show();
}
}
};
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
mProgress.dismiss();
if (msg.what == 1) {
Toast.makeText(MainActivity.this, "Facebook logout failed",
Toast.LENGTH_SHORT).show();
} else {
mFacebookBtn.setChecked(false);
mFacebookBtn.setText(" Facebook (Not connected)");
Toast.makeText(MainActivity.this, "Disconnected from Facebook",
Toast.LENGTH_SHORT).show();
}
}
};
private class GetRequestTokenTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
twitter = TwitterFactory.getSingleton();
twitter.setOAuthConsumer("UUyRw5Xq9zVUtToGQopqaDtqP","1uZcuyB5OBKFsWi1Nb8PZsxQ0VlllxT4EJvCscE7ejjs47GjGh");
try {
// launch login activity
RequestToken requestToken = twitter.getOAuthRequestToken(getString(R.string.TWITTER_CALLBACK_URL));
launchLoginWebView(requestToken);
} catch (TwitterException e) {
e.printStackTrace();
}
return null;
}
}
private void loginToTwitter() {
// starting class to get access token
GetRequestTokenTask getRequestTokenTask = new GetRequestTokenTask();
getRequestTokenTask.execute();
}
private void launchLoginWebView(RequestToken requestToken) {
// intent to launch activity + sending request
Intent intent = new Intent(this, Logintwitter.class);
intent.putExtra(MainActivity.AUTHENTICATION_URL_KEY,
requestToken.getAuthenticationURL());
startActivityForResult(intent, LOGIN_TO_TWITTER_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == LOGIN_TO_TWITTER_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
getAccessToken(data
.getStringExtra(Logintwitter.CALLBACK_URL_KEY));
}
}
}
private void getAccessToken(String callbackUrl) {
Uri uri = Uri.parse(callbackUrl);
String verifier = uri.getQueryParameter("oauth_verifier");
GetAccessTokenTask getAccessTokenTask = new GetAccessTokenTask();
getAccessTokenTask.execute(verifier);
}
private class GetAccessTokenTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
String verifier = strings[0];
try {
accessToken = twitter.getOAuthAccessToken(verifier);
Log.d(MainActivity.class.getSimpleName(), accessToken.getToken());
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(Void result){
mSharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE);
Editor editor = mSharedPreferences.edit();
editor.putString(ACCESS_TOKEN , accessToken.getToken());
editor.putString(ACCESS_SECRET, accessToken.getTokenSecret() );
editor.commit();
Toast.makeText(getApplicationContext(), "hola", 54).show();
super.onPostExecute(result);
}
}
}
点击此按钮
时出现错误twitterbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
loginToTwitter();
String token = "" + accessToken;
//Toast.makeText(getApplicationContext(), token, 500).show();
}
});
当我点击这个按钮时它强制关闭这是日志猫
05-26 18:30:05.943: E/AndroidRuntime(10638): FATAL EXCEPTION: AsyncTask #1
05-26 18:30:05.943: E/AndroidRuntime(10638): Process: pidevelopers.floata, PID: 10638
05-26 18:30:05.943: E/AndroidRuntime(10638): java.lang.RuntimeException: An error occured while executing doInBackground()
05-26 18:30:05.943: E/AndroidRuntime(10638): at android.os.AsyncTask$3.done(AsyncTask.java:300)
05-26 18:30:05.943: E/AndroidRuntime(10638): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
05-26 18:30:05.943: E/AndroidRuntime(10638): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
05-26 18:30:05.943: E/AndroidRuntime(10638): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
05-26 18:30:05.943: E/AndroidRuntime(10638): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-26 18:30:05.943: E/AndroidRuntime(10638): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-26 18:30:05.943: E/AndroidRuntime(10638): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-26 18:30:05.943: E/AndroidRuntime(10638): at java.lang.Thread.run(Thread.java:841)
05-26 18:30:05.943: E/AndroidRuntime(10638): Caused by: java.lang.NoClassDefFoundError: twitter4j.TwitterFactory
05-26 18:30:05.943: E/AndroidRuntime(10638): at pidevelopers.floata.MainActivity$GetRequestTokenTask.doInBackground(MainActivity.java:278)
05-26 18:30:05.943: E/AndroidRuntime(10638): at pidevelopers.floata.MainActivity$GetRequestTokenTask.doInBackground(MainActivity.java:1)
05-26 18:30:05.943: E/AndroidRuntime(10638): at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-26 18:30:05.943: E/AndroidRuntime(10638): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-26 18:30:05.943: E/AndroidRuntime(10638): ... 4 more
请注意,此代码适用于没有facebook的其他项目 所有需要的权限都已添加
问题是,当我点击推特按钮时,它会强行关闭
答案 0 :(得分:0)
我有同样的问题..几天后我发现了问题:
// We cannot define our own keys for Javascript arrays, so we are using Object
var result = new Object();
var salesIndex = 8;
var idIndex = 0;
var ndiIndex = 7;
for(var i=0; i< array1.length; i++){
// array containing all line's columns
var columns = array1[i].split(",");
// especific columns that is of our interest
var rowSale = columns[salesIndex];
var rowId = columns[idIndex];
var rowNdi = columns[ndiIndex];
// If sale key doesn't exists in the array, add it
if (!(rowSale in columns)) {
result.rowSale.rowId = new Array();
result.rowSale.rowId.push(rowNdi);
} else if (!(rowId in columns.rowSale)) {
// If id key doesn't exists in the array, add it
result.rowSale.rowId = new Array();
result.rowSale.rowId.push(rowNdi);
} else if (!columns.rowSale.rowId.indexOf(rowNdi)) {
// If ndi isn't in the array, add it
result.rowSale.rowId.push(rowNdi);
}
}
noHistory =“true”导致twitter登录后关闭力