制作推特Android应用程序。试图让OAuth工作。在尝试初始化消费者时,我得到了一个nullpointerexception。
这是我的主要活动加上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) {
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);
}
}
这是我的logcat:
04-16 13:03:54.976: I/com.example.twittertestnewapi.MainActivity(22476): test
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): Error creating consumer / provider
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): java.lang.NullPointerException
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at android.support.v4.content.Loader.<init>(Loader.java:91)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at android.support.v4.content.AsyncTaskLoader.<init>(AsyncTaskLoader.java:92)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at com.example.twittertestnewapi.getCommonsHttpOAuthConsumer.<init>(MainActivity.java:115)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at com.example.twittertestnewapi.MainActivity.onCreateLoader(MainActivity.java:87)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at android.support.v4.app.LoaderManagerImpl.createLoader(LoaderManager.java:490)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at android.support.v4.app.LoaderManagerImpl.createAndInstallLoader(LoaderManager.java:499)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at android.support.v4.app.LoaderManagerImpl.initLoader(LoaderManager.java:553)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at com.example.twittertestnewapi.MainActivity.sign_in(MainActivity.java:68)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at com.example.twittertestnewapi.MainActivity$1.onClick(MainActivity.java:56)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at android.view.View.performClick(View.java:4204)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at android.view.View$PerformClick.run(View.java:17355)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at android.os.Handler.handleCallback(Handler.java:725)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at android.os.Looper.loop(Looper.java:137)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at java.lang.reflect.Method.invoke(Method.java:511)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-16 13:03:54.986: E/com.example.twittertestnewapi.MainActivity(22476): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您没有初始化与context
一起使用的Loader
变量。在onCreate()
方法中添加:
context = this;