Another night another problem w/ PreferencesActivity implementation
Forums
»
Android - Learning Android book
>
Another night another problem w/ PreferencesActivity...
Ethan Behar
UAT
Member since Jan 30, 2012
Forum Posts: 7
Forum Posts: 7
Hey all,
So, tonight I implemented the menu bar and preferencesActivity. The
app works great it still updates the status to yamba.marakana.com but
one thing stopped working. On the twitter update thread the
onPostExecute function Marko made would pop up a toast box that would
say if the status fail or succed. This is the part that broke for me
and its very frustating because none of the code I implemented
changed this(atleast I don't think it does). Now I did change this
code around a little bit becuase orginially the toast text would be
the status i wrote or "Failure to Post". So I changed it to say
"Successful Post!" Anyways I'll post my code, I'll still be looking
into this issue tonight but the help of others is always welcomed!
Thanks alot marakana community!
Thread Code: The onPostExecute(String result) function is the one that stopped displaying the toast box.
Twitter code and new OnClick function:
Menu Code
Class definition and onCreate method:
So, tonight I implemented the menu bar and preferencesActivity. The
app works great it still updates the status to yamba.marakana.com but
one thing stopped working. On the twitter update thread the
onPostExecute function Marko made would pop up a toast box that would
say if the status fail or succed. This is the part that broke for me
and its very frustating because none of the code I implemented
changed this(atleast I don't think it does). Now I did change this
code around a little bit becuase orginially the toast text would be
the status i wrote or "Failure to Post". So I changed it to say
"Successful Post!" Anyways I'll post my code, I'll still be looking
into this issue tonight but the help of others is always welcomed!
Thanks alot marakana community!
Thread Code: The onPostExecute(String result) function is the one that stopped displaying the toast box.
Code:
class PostToTwitter extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... statuses) {
try {
winterwell.jtwitter.Status status = twitter
.updateStatus(statuses[0]);
return status.text;
} catch (TwitterException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
return "Failed to post!";
}
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
String successResult = "Successful Post!";
if (result == "Failed to post!") {
Toast.makeText(StatusActivity.this, result, Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(StatusActivity.this, successResult,
Toast.LENGTH_LONG).show();
}
}
}
Twitter code and new OnClick function:
Code:
// get Twitter Info
private Twitter getTwitter() {
if (twitter == null) {
String username = preferences.getString("username", "");
String password = preferences.getString("password", "");
String APIRoot = preferences.getString("APIRoot",
"http://yamba.marakana.com/api");
twitter = new Twitter(username, password);
twitter.setAPIRootUrl(APIRoot);
}
return twitter;
}
//reset twitter info when username/password is changed in preferencesActivity
public void onSharedPreferenceChanged(SharedPreferences preferences, String key){
twitter = null;
}
public void onClick(View inView) {
try{
getTwitter().setStatus(editText.getText().toString());
} catch (TwitterException e) {
Log.d(TAG, "Twitter setStatus failed: " + e);
}
}
Menu Code
Code:
// menu options stuff
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.itemPreferences:
startActivity(new Intent(this, PreferencesActivity.class));
break;
}
return true;
}
Class definition and onCreate method:
Code:
public class StatusActivity extends Activity implements OnClickListener,
TextWatcher, OnSharedPreferenceChangeListener {
private static final String TAG = "StatusActivity";
SharedPreferences preferences;
EditText editText;
Button btnUpdate;
Twitter twitter;
TextView textCount;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status);
// find views in stauts.xml
// tell button to listen for its click
editText = (EditText) findViewById(R.id.editText);
btnUpdate = (Button) findViewById(R.id.buttonUpdate);
btnUpdate.setOnClickListener(this);
textCount = (TextView) findViewById(R.id.textCounter);
textCount.setText(Integer.toString(140));
textCount.setTextColor(Color.GREEN);
editText.addTextChangedListener(this);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.registerOnSharedPreferenceChangeListener(this);
}
Marko Gargenta@MarkoGargenta
Marakana, Inc.
Member since Jan 19, 2007
Location: San Francisco
Forum Posts: 227
Location: San Francisco
Forum Posts: 227
Ethan, can you please post the actual exception that breaks the app. The answer is in there, most likely. Use
Cheers,
Marko
adb logcat and look for last exception. Cheers,
Marko
Ethan Behar
UAT
Member since Jan 30, 2012
Forum Posts: 7
Forum Posts: 7
Ok here are the logs in StatusActivity after I re-installed the app and updated the status 3 times.
02-01 19:25:16.182: D/StatusActivity(5288): Twitter setStatus failed: winterwell.jtwitter.TwitterException$E401: Could not authenticate you.
02-01 19:25:16.182: D/StatusActivity(5288): http://yamba.marakana.com/api/statuses/update.json ()
02-01 19:25:47.933: D/StatusActivity(5288): Twitter setStatus failed: winterwell.jtwitter.TwitterException$E401: Could not authenticate you.
02-01 19:25:47.933: D/StatusActivity(5288): http://yamba.marakana.com/api/statuses/update.json ()
This was because I didn't set my username and password. When the program runs it doesn't crash or throw an exception. It's just the toast boxes that don't pop up anymore. But before the implementation of the menu bar the toast boxes popped up fine when I posted a status.
-Ethan
02-01 19:25:16.182: D/StatusActivity(5288): Twitter setStatus failed: winterwell.jtwitter.TwitterException$E401: Could not authenticate you.
02-01 19:25:16.182: D/StatusActivity(5288): http://yamba.marakana.com/api/statuses/update.json ()
02-01 19:25:47.933: D/StatusActivity(5288): Twitter setStatus failed: winterwell.jtwitter.TwitterException$E401: Could not authenticate you.
02-01 19:25:47.933: D/StatusActivity(5288): http://yamba.marakana.com/api/statuses/update.json ()
This was because I didn't set my username and password. When the program runs it doesn't crash or throw an exception. It's just the toast boxes that don't pop up anymore. But before the implementation of the menu bar the toast boxes popped up fine when I posted a status.
-Ethan
Ethan Behar
UAT
Member since Jan 30, 2012
Forum Posts: 7
Forum Posts: 7
Okay, so I just stumbled upon something interesting... I put logs in all of the functions of
PostToTwitter class and none of them appeared in LogCat... So, does this mean that posting to yamba.marakana.com isn't being ran on a separate thread somehow? And if so why isn't my app crashing due to the sluggish nature of posting a status? Check out my logs.. TAG2 = "onPostExecute"
PostToTwitter class and none of them appeared in LogCat... So, does this mean that posting to yamba.marakana.com isn't being ran on a separate thread somehow? And if so why isn't my app crashing due to the sluggish nature of posting a status? Check out my logs.. TAG2 = "onPostExecute"
Code:
// Asynchronously posts to twitter
class PostToTwitter extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... statuses) {
try {
Log.d(TAG2, "in try doInBackground");
winterwell.jtwitter.Status status = twitter
.updateStatus(statuses[0]);
return status.text;
} catch (TwitterException e) {
Log.d(TAG2, "in catch doInBackground");
Log.e(TAG, e.toString());
e.printStackTrace();
return "Failed to post!";
}
}
@Override
protected void onProgressUpdate(Integer... values) {
Log.d(TAG2, "In onProgressUpdate");
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Log.d(TAG2, "Imade it to onPostExecute");
String successResult = "Successful Post!";
if (result == "Failed to post!") {
Log.d(TAG2, "Imade it to onPostExecute/FAIL");
Toast.makeText(StatusActivity.this, result, Toast.LENGTH_LONG)
.show();
} else {
Log.d(TAG2, "Imade it to onPostExecute/SUCCESS");
Toast.makeText(StatusActivity.this, successResult,
Toast.LENGTH_LONG).show();
}
Log.d(TAG2, "leaving onPostExecute");
}
}
Ethan Behar
UAT
Member since Jan 30, 2012
Forum Posts: 7
Forum Posts: 7
I figured it out... The toast box wasn't popping up anymore because when onClick was called I wasn't creating a PostToTwitter object.