不幸的是,APP已停止 - Android Twitter客户端

时间:2013-12-17 08:07:01

标签: java android eclipse twitter

我正在根据教程为Android创建一个Twitter客户端。当我编译它,然后按菜单对twitter进行身份验证时,应用程序突然停止并显示警告“不幸的是,APP已停止”。

以下是该程序的logcat:

12-17 17:02:07.025: E/AndroidRuntime(24713): FATAL EXCEPTION: main
12-17 17:02:07.025: E/AndroidRuntime(24713): java.lang.NoClassDefFoundError: oauth.signpost.commonshttp.CommonsHttpOAuthConsumer
12-17 17:02:07.025: E/AndroidRuntime(24713):    at com.tmm.android.twitter.AuthActivity.askOAuth(AuthActivity.java:106)
12-17 17:02:07.025: E/AndroidRuntime(24713):    at com.tmm.android.twitter.AuthActivity.access$0(AuthActivity.java:104)
12-17 17:02:07.025: E/AndroidRuntime(24713):    at com.tmm.android.twitter.AuthActivity$1.onClick(AuthActivity.java:53)
12-17 17:02:07.025: E/AndroidRuntime(24713):    at android.view.View.performClick(View.java:4223)
12-17 17:02:07.025: E/AndroidRuntime(24713):    at android.view.View$PerformClick.run(View.java:17275)
12-17 17:02:07.025: E/AndroidRuntime(24713):    at android.os.Handler.handleCallback(Handler.java:615)
12-17 17:02:07.025: E/AndroidRuntime(24713):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-17 17:02:07.025: E/AndroidRuntime(24713):    at android.os.Looper.loop(Looper.java:137)
12-17 17:02:07.025: E/AndroidRuntime(24713):    at android.app.ActivityThread.main(ActivityThread.java:4898)
12-17 17:02:07.025: E/AndroidRuntime(24713):    at java.lang.reflect.Method.invokeNative(Native Method)
12-17 17:02:07.025: E/AndroidRuntime(24713):    at java.lang.reflect.Method.invoke(Method.java:511)
12-17 17:02:07.025: E/AndroidRuntime(24713):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
12-17 17:02:07.025: E/AndroidRuntime(24713):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
12-17 17:02:07.025: E/AndroidRuntime(24713):    at dalvik.system.NativeStart.main(Native Method)

这是主要的课程:

package com.tmm.android.twitter;


import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.tmm.android.twitter.appliaction.TwitterApplication;
import com.tmm.android.twitter.util.Constants;



public class AuthActivity extends Activity {

    private Twitter twitter;
    private OAuthProvider provider;
    private CommonsHttpOAuthConsumer consumer;

    private String CONSUMER_KEY =           "CV78QI1eZAgGe9mMS7A";
    private String CONSUMER_SECRET =        "zCYkLsHlAVf7DoWJlDbfqeFsghXQCu2dUaFC2O94HQ";
    private String CALLBACK_URL =           "callback://tweeter";

    private Button buttonLogin;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        System.setProperty("http.keepAlive", "false");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_oauth);

        //check for saved log in details..
        checkForSavedLogin();

        //set consumer and provider on teh Application service
        getConsumerProvider();

        //Define login button and listener
        buttonLogin = (Button)findViewById(R.id.ButtonLogin);
        buttonLogin.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {
                askOAuth();
            }
        });
    }

    private void checkForSavedLogin() {
        // Get Access Token and persist it
        AccessToken a = getAccessToken();
        if (a==null) return;    //if there are no credentials stored then return to usual activity

        // initialize Twitter4J
        twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
        twitter.setOAuthAccessToken(a);
        ((TwitterApplication)getApplication()).setTwitter(twitter);

        startFirstActivity();
        finish();
    }

    /**
     * Kick off the activity to display 
     */
    private void startFirstActivity() {
        System.out.println("STARTING FIRST ACTIVITY!");
        Intent i = new Intent(this, TweetsActivity.class);
        startActivityForResult(i, Constants.ACTIVITY_LATEST_TWEETS);
    }

    /**
     * This method checks the shared prefs to see if we have persisted a user token/secret
     * if it has then it logs on using them, otherwise return null
     * 
     * @return AccessToken from persisted prefs
     */
    private AccessToken getAccessToken() {
        SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE);
        String token = settings.getString("accessTokenToken", "");
        String tokenSecret = settings.getString("accessTokenSecret", "");
        if (token!=null && tokenSecret!=null && !"".equals(tokenSecret) && !"".equals(token)){
            return new AccessToken(token, tokenSecret);
        }
        return null;
    }



    /**
     * Open the browser and asks the user to authorize the app.
     * Afterwards, we redirect the user back here!
     */
    private void askOAuth() {
        try {
            consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
            provider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token", "http://twitter.com/oauth/access_token", "http://twitter.com/oauth/authorize");
            String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
            Toast.makeText(this, "Please authorize this app!", Toast.LENGTH_LONG).show();
            setConsumerProvider();
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
        } catch (Exception e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }


    /**
     * As soon as the user successfully authorized the app, we are notified
     * here. Now we need to get the verifier from the callback URL, retrieve
     * token and token_secret and feed them to twitter4j (as well as
     * consumer key and secret).
     */
    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("RESUMING!!");
        if (this.getIntent()!=null && this.getIntent().getData()!=null){
            Uri uri = this.getIntent().getData();
            if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
                String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
                try {
                    // this will populate token and token_secret in consumer
                    provider.retrieveAccessToken(consumer, verifier);

                    // Get Access Token and persist it
                    AccessToken a = new AccessToken(consumer.getToken(), consumer.getTokenSecret());
                    storeAccessToken(a);

                    // initialize Twitter4J
                    twitter = new TwitterFactory().getInstance();
                    twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
                    twitter.setOAuthAccessToken(a);
                    ((TwitterApplication)getApplication()).setTwitter(twitter);
                    //Log.e("Login", "Twitter Initialised");

                    startFirstActivity();

                } catch (Exception e) {
                    //Log.e(APP, e.getMessage());
                    e.printStackTrace();
                    Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        }
    }

    /**
     * This method persists the Access Token information so that a user
     * is not required to re-login every time the app is used
     * 
     * @param a - the access token
     */
    private void storeAccessToken(AccessToken a) {
        SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("accessTokenToken", a.getToken());
        editor.putString("accessTokenSecret", a.getTokenSecret());
        editor.commit();
    }


    /**
     * Get the consumer and provider from the application service (in the case that the
     * activity is restarted so the objects are not lost
     */
    private void getConsumerProvider() {
        OAuthProvider p = ((TwitterApplication)getApplication()).getProvider();
        if (p!=null){
            provider = p;
        }
        CommonsHttpOAuthConsumer c = ((TwitterApplication)getApplication()).getConsumer();
        if (c!=null){
            consumer = c;
        }
    }


    /**
     * Set the consumer and provider from the application service (in the case that the
     * activity is restarted so the objects are not lost)
     */
    private void setConsumerProvider() {
        if (provider!=null){
            ((TwitterApplication)getApplication()).setProvider(provider);
        }
        if (consumer!=null){
            ((TwitterApplication)getApplication()).setConsumer(consumer);
        }
    }

}

任何帮助都会非常感激

1 个答案:

答案 0 :(得分:1)

试试这个:

转到项目/属性/ Java构建路径/订单和导出 - 如果您使用它,请确保在Android Dependencies和支持库前面进行检查。标记所有复选框并单击“应用”并清除项目。

希望这有帮助。