Reputation: 7053
package com.markana.yamba;
import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.TwitterException;
import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class StatusActivity2 extends Activity implements OnClickListener{
private static final String TAG= "StatusActivity";
EditText editText;
Button updateButton;
TextView textCount;
Twitter twitter;
/** Called when the activity is first created. */
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status);
//Finds views
editText =(EditText) findViewById(R.id.editText);
updateButton=(Button) findViewById(R.id.buttonUpdate);
updateButton.setOnClickListener(this);
twitter=new Twitter("student","password");
twitter.setAPIRootUrl(("http://yamba.marakana.com/api"));
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
String status=editText.getText().toString();
new PostToTwitter().execute(status);
Log.d(TAG, "onClick");
}
class PostToTwitter extends AsyncTask<String,Integer, String>
{
@Override
protected String doInBackground(String... statuses) {
try{
Twitter.Status status=twitter.updateStatus(statuses[0]);
return status.text;
}
catch(TwitterException e)
{
Log.e(TAG,e.toString());
e.printStackTrace();
return "Failed to post";
}
}
protected void onProgressUpdate(Integer...values)
{
super.onProgressUpdate(values);
}
protected void onPostExecute(String result)
{
Toast.makeText(StatusActivity2.this,result,Toast.LENGTH_LONG).show();
}
}
}
i get two exceptions relating to Async task
thats what i did. it tells me that i have a problem when i run the background worker!!
04-20 09:21:32.553: ERROR/AndroidRuntime(838): FATAL EXCEPTION: AsyncTask #1
04-20 09:21:32.553: ERROR/AndroidRuntime(838): java.lang.RuntimeException: An error occured while executing doInBackground()
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.lang.Thread.run(Thread.java:1096)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): Caused by: java.lang.IllegalArgumentException
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.Date.parse(Date.java:447)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.Date.<init>(Date.java:157)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at winterwell.jtwitter.Twitter$Status.<init>(Twitter.java:659)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at winterwell.jtwitter.Twitter.updateStatus(Twitter.java:3231)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at winterwell.jtwitter.Twitter.updateStatus(Twitter.java:3161)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at com.markana.yamba.StatusActivity2$PostToTwitter.doInBackground(StatusActivity2.java:60)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at com.markana.yamba.StatusActivity2$PostToTwitter.doInBackground(StatusActivity2.java:1)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): ... 4 more
it should pop a message in the end.. as you can see in the method onClick!
Upvotes: 1
Views: 1615
Reputation: 1303
I don't think it has anything to do with AsyncTask or Context. The below line is failing which is doing some date parsing. Can you try calling below in a main thread without AsyncTask and see if it is working?.
Twitter.Status status=twitter.updateStatus(statuses[0]);
Upvotes: 0
Reputation: 1336
Twitter.java:659
are you using some date creation here ? looks like you are parsing a string to date object and string is not in the date format specified.
below is ur problem part. nothing to do with task creation i guess.what format is 04-20 09:21:32.553
i guess it should be somewat like mm-dd-yy hh:mm:ss.nnn or some other standard format
Caused by: java.lang.IllegalArgumentException
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.Date.parse(Date.java:447)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at java.util.Date.<init>(Date.java:157)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at winterwell.jtwitter.Twitter$Status.<init>(Twitter.java:659)
04-20 09:21:32.553: ERROR/AndroidRuntime(838): at winterwell.jtwitter.Twitter.updateStatus(Twitter.java:3231)
Upvotes: 1
Reputation: 30934
It may be that the
Toast.makeText(StatusActivity2.this,res..
fails, as the context you pass in is not valid.
Try to pass in the Context
in the Constructor of PostToTwitter
, store it locally and then use this instance when creating the Toast
.
See e.g. here for the call, here for the Constructor and here for the onPostExecute
Upvotes: 0
Reputation: 28541
Assuming I understand properly: update your screen in the onPostExecute method. You might want to have a member variable of your activity holding the current task. The in your onClick function, simply check if a task is already running before creating a new one.
Also, don't forget to remove the click listener when you destroy your activity, else you will leak memory.
protected void onDestroy() {
updateButton.setOnClickListener(null);
super.onDestroy();
}
Upvotes: 0