0

在我的 android 应用程序中,数据必须使用 twitter 共享,所以按照这个链接http://androidcodeexamples.blogspot.in/2011/12/how-to-integrate-twitter-in-android.html,一切正常,但是提供用户名和密码后,获得强制关闭选项。这是 logcat 输出

  01-17 17:07:54.118: E/AndroidRuntime(383): FATAL EXCEPTION: main
  01-17 17:07:54.118: E/AndroidRuntime(383): java.lang.NullPointerException
 01-17 17:07:54.118: E/AndroidRuntime(383):      com.twitter.android.TwitterApp$1.handleMessage(TwitterApp.java:237)
 01-17 17:07:54.118: E/AndroidRuntime(383):     at     android.os.Handler.dispatchMessage(Handler.java:99)
01-17 17:07:54.118: E/AndroidRuntime(383):  at android.os.Looper.loop(Looper.java:123)
01-17 17:07:54.118: E/AndroidRuntime(383):  at   android.app.ActivityThread.main(ActivityThread.java:3683)
01-17 17:07:54.118: E/AndroidRuntime(383):  at  java.lang.reflect.Method.invokeNative(Native Method)
01-17 17:07:54.118: E/AndroidRuntime(383):  at java.lang.reflect.Method.invoke(Method.java:507)
01-17 17:07:54.118: E/AndroidRuntime(383):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-17 17:07:54.118: E/AndroidRuntime(383):  at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 01-17 17:07:54.118: E/AndroidRuntime(383):     at   dalvik.system.NativeStart.main(Native Method)

这是mainactivity中的代码

public class Singlemenuitem  extends Activity {

private TwitterApp mTwitter;
private static final String CONSUMER_KEY = "6JyIkj71ZqG4wk3YF0Y4hw";
private static final String CONSUMER_SECRET = "sJl9aRVqlEt7nxlKvpMVK6tLULz5FSQ2KUOW0yie4";
private enum FROM {
    TWITTER_POST, TWITTER_LOGIN
};

private enum MESSAGE {
    SUCCESS, DUPLICATE, FAILED, CANCELLED
};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_list_item);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
    .permitAll().build();
   StrictMode.setThreadPolicy(policy);

   mTwitter = new TwitterApp(this, CONSUMER_KEY, CONSUMER_SECRET);

    //share twitter
        final ImageView twitter = (ImageView) findViewById(R.id.twitter);
        twitter.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v){
                mTwitter.resetAccessToken();
        if (mTwitter.hasAccessToken() == true) {
            try {
                mTwitter.updateStatus(String.valueOf(Html
                        .fromHtml(TwitterApp.MESSAGE)));

                // File f = new File("/mnt/sdcard/android.jpg");
                // mTwitter.uploadPic(f, String.valueOf(Html
                // .fromHtml(TwitterApp.MESSAGE)));

                postAsToast(FROM.TWITTER_POST, MESSAGE.SUCCESS);
            } catch (Exception e) {
                if (e.getMessage().toString().contains("duplicate")) {
                    postAsToast(FROM.TWITTER_POST, MESSAGE.DUPLICATE);
                }
                e.printStackTrace();
            }
            mTwitter.resetAccessToken();
        } else {
            mTwitter.authorize();
        }
    }

    private void postAsToast(FROM twitterPost, MESSAGE success) {
        switch (twitterPost) {
        case TWITTER_LOGIN:
            switch (success) {
            case SUCCESS:
Toast.makeText(Singlemenuitem.this, "Login Successful", Toast.LENGTH_LONG)
                        .show();
                break;
            case FAILED:
                Toast.makeText(Singlemenuitem.this, "Login Failed", Toast.LENGTH_LONG).show();
            default:
                break;
            }
            break;
        case TWITTER_POST:
            switch (success) {
            case SUCCESS:
                Toast.makeText(Singlemenuitem.this, "Posted Successfully", Toast.LENGTH_LONG)
                        .show();
                break;
            case FAILED:
                Toast.makeText(Singlemenuitem.this, "Posting Failed", Toast.LENGTH_LONG)
                        .show();
                break;
            case DUPLICATE:
                Toast.makeText(Singlemenuitem.this,
                        "Posting Failed because of duplicate message...",
                        Toast.LENGTH_LONG).show();
            default:
                break;
            }
            break;
        }


    TwDialogListener mTwLoginDialogListener = new TwDialogListener() {

        public void onError(String value) {
            postAsToast(FROM.TWITTER_LOGIN, MESSAGE.FAILED);
            Log.e("TWITTER", value);
            mTwitter.resetAccessToken();
        }

        public void onComplete(String value) {
            try {
                mTwitter.updateStatus(TwitterApp.MESSAGE);
                postAsToast(FROM.TWITTER_POST, MESSAGE.SUCCESS);
            } catch (Exception e) {
                if (e.getMessage().toString().contains("duplicate")) {
                    postAsToast(FROM.TWITTER_POST, MESSAGE.DUPLICATE);
                }
                e.printStackTrace();
            }
            mTwitter.resetAccessToken();
        }
4

0 回答 0