0

制作一个 twitter 安卓应用。试图使 OAuth 工作。我在尝试初始化消费者时收到 java.lang.ClassCastException。

这是我的主要活动以及 asynctaskloader 类:

public class MainActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks{

final String TAG = getClass().getName();

private OAuthConsumer consumer;
private OAuthProvider provider;
private String token;
private String secret;
private getCommonsHttpOAuthConsumer consumerLoader;

Activity context;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button signInButton = (Button) findViewById(R.id.signInButton);

    signInButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            sign_in();
        }
    });     
}


public void sign_in()
{
    try {

        Log.i(TAG, "test");

        this.consumer = (OAuthConsumer) getSupportLoaderManager().initLoader(0, null, this); //here's the error

        Log.i(TAG, "test2");
    } catch (Exception e) {
        Log.e(TAG, "Error creating consumer / provider",e);
    }       
}


@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;
}


@Override
public android.support.v4.content.Loader onCreateLoader(int id, Bundle arg1) {
    context = this;

    consumerLoader = new getCommonsHttpOAuthConsumer(context);
    return consumerLoader;
}


@Override
public void onLoadFinished(android.support.v4.content.Loader arg0,
        Object arg1) {
    // TODO Auto-generated method stub

}


@Override
public void onLoaderReset(android.support.v4.content.Loader arg0) {
    // TODO Auto-generated method stub

}

}





class getCommonsHttpOAuthConsumer extends AsyncTaskLoader<OAuthConsumer>{

public getCommonsHttpOAuthConsumer(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

@Override
public OAuthConsumer loadInBackground() {
    // TODO Auto-generated method stub

    return new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
}

}

这是我的日志:

04-16 15:34:57.819: I/com.example.twittertestnewapi.MainActivity(28164): test
04-16 15:34:57.819: E/com.example.twittertestnewapi.MainActivity(28164): Error creating consumer / provider
04-16 15:34:57.819: E/com.example.twittertestnewapi.MainActivity(28164): java.lang.ClassCastException: com.example.twittertestnewapi.getCommonsHttpOAuthConsumer cannot be cast to oauth.signpost.OAuthConsumer
04-16 15:34:57.819: E/com.example.twittertestnewapi.MainActivity(28164):    at com.example.twittertestnewapi.MainActivity.sign_in(MainActivity.java:68)
04-16 15:34:57.819: E/com.example.twittertestnewapi.MainActivity(28164):    at com.example.twittertestnewapi.MainActivity$1.onClick(MainActivity.java:56)
04-16 15:34:57.819: E/com.example.twittertestnewapi.MainActivity(28164):    at android.view.View.performClick(View.java:4204)
04-16 15:34:57.819: E/com.example.twittertestnewapi.MainActivity(28164):    at android.view.View$PerformClick.run(View.java:17355)
04-16 15:34:57.819: E/com.example.twittertestnewapi.MainActivity(28164):    at android.os.Handler.handleCallback(Handler.java:725)
04-16 15:34:57.819: E/com.example.twittertestnewapi.MainActivity(28164):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 15:34:57.819: E/com.example.twittertestnewapi.MainActivity(28164):    at android.os.Looper.loop(Looper.java:137)
04-16 15:34:57.819: E/com.example.twittertestnewapi.MainActivity(28164):    at android.app.ActivityThread.main(ActivityThread.java:5041)
04-16 15:34:57.819: E/com.example.twittertestnewapi.MainActivity(28164):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 15:34:57.819: E/com.example.twittertestnewapi.MainActivity(28164):    at java.lang.reflect.Method.invoke(Method.java:511)
04-16 15:34:57.819: E/com.example.twittertestnewapi.MainActivity(28164):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-16 15:34:57.819: E/com.example.twittertestnewapi.MainActivity(28164):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-16 15:34:57.819: E/com.example.twittertestnewapi.MainActivity(28164):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

确保您的 OAuthConsumer 类实现了 Loader 回调接口。

于 2013-12-12T12:16:45.870 回答