Twitter API と非同期タスクの初心者です。シンプルなAPIと思われるjTwitterを使ってOAuthを試していました。メインスレッドでネットワーク操作を行うことはできないため。仕事に asynctask を作成しました。理解できない例外がスローされます。例外を解決するのを手伝ってください。
ここにコードがあります
Twitter twitter = null;
private EditText tweetTxt = null;
private Button postBtn = null;
private OAuthSignpostClient client = null;
private String CALLBACK_URI = "myapp://twitt";
private String authUrl = null;
private ProgressDialog postDialog = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
postBtn=(Button)findViewById(R.id.button1);
postBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
new MyTask().execute();
}
});
}
private class MyTask extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
Toast.makeText(getApplicationContext(), "pre execute", Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... arg0) {
client = new OAuthSignpostClient("" ,"", CALLBACK_URI);
authUrl = client.authorizeUrl().toString();
getApplicationContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
//Check if you got NewIntent event due to Twitter Call back only
if (uri != null && uri.toString().startsWith(CALLBACK_URI))
{
try
{
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
client.setAuthorizationCode(verifier);
}
catch(Exception e){
Log.d("exception", e.getMessage());
}
}}
そして私が得る例外
02-17 10:58:54.751: E/AndroidRuntime(987): FATAL EXCEPTION: AsyncTask #1
02-17 10:58:54.751: E/AndroidRuntime(987): java.lang.RuntimeException: An error occured while executing doInBackground()
02-17 10:58:54.751: E/AndroidRuntime(987): at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-17 10:58:54.751: E/AndroidRuntime(987): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-17 10:58:54.751: E/AndroidRuntime(987): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-17 10:58:54.751: E/AndroidRuntime(987): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-17 10:58:54.751: E/AndroidRuntime(987): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-17 10:58:54.751: E/AndroidRuntime(987): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-17 10:58:54.751: E/AndroidRuntime(987): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-17 10:58:54.751: E/AndroidRuntime(987): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-17 10:58:54.751: E/AndroidRuntime(987): at java.lang.Thread.run(Thread.java:856)
02-17 10:58:54.751: E/AndroidRuntime(987): Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
02-17 10:58:54.751: E/AndroidRuntime(987): at android.app.ContextImpl.startActivity(ContextImpl.java:886)
02-17 10:58:54.751: E/AndroidRuntime(987): at android.app.ContextImpl.startActivity(ContextImpl.java:880)
02-17 10:58:54.751: E/AndroidRuntime(987): at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
02-17 10:58:54.751: E/AndroidRuntime(987): at com.example.retrieve.contacts.twitter.MainActivity$MyTask.doInBackground(MainActivity.java:72)