1
我想創建一個簡單的twitter更新應用程序android。我發現在http://marakana.com/forums/android/examples/69.html。 下面是代碼MyTwitter.Java:android twitter應用:暫停(例外(推特例外))
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import winterwell.jtwitter.Twitter;
public class MyTwitter extends Activity implements OnClickListener
{
static final String TAG = "MyTwitter";
Twitter twitter;
SharedPreferences prefs;
Button buttonUpdate;
Button buttonPrefs;
EditText textStatus;
@Override
public void onClick(View src)
{
String status = textStatus.getText().toString();
Log.d(TAG, "Clicked on "+ status);
// Toast
Toast.makeText(this, textStatus.getText(), Toast.LENGTH_LONG).show();
// set twitter status
twitter.setStatus(status);
//reset status string
textStatus.setText("");
}
/** Called when the activity is first created. */
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// find views by id
buttonUpdate = (Button) findViewById(R.id.buttonUpdate);
textStatus = (EditText) findViewById(R.id.textStatus);
// Add listener
buttonUpdate.setOnClickListener(this);
//Initialize twitter
prefs = PreferenceManager.getDefaultSharedPreferences(this);
String username = prefs.getString("username", "n/a");
String password = prefs.getString("password", "n/a");
if (username != null && password != null){
twitter = new Twitter(username, password);
}
}
@Override
// when the menu button is pressed another layout is shown. that is menu
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
// Called when menu item is selected //
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId()){
case R.id.menuPrefs:
// Launch Prefs activity
Intent i = new Intent(MyTwitter.this, Prefs.class);
startActivity(i);
Log.d(TAG, "MenuPrefs starting Prefs");
Toast.makeText(MyTwitter.this, textStatus.getText(), Toast.LENGTH_LONG).show();
break;
}
return true;
}
}
,另一種是Prefs.java:
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Prefs extends PreferenceActivity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
這裏是主要佈局的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">
<TextView android:text="@string/labelWhatsHappening" android:layout_height="wrap_content" android:textStyle="bold" android:layout_width="fill_parent" android:textSize="30sp" android:gravity="center"></TextView>
<EditText
android:id="@+id/textStatus"
android:typeface="serif"
android:textColorHint="#E6E6E6"
android:textColor="#E6E6E6"
android:maxLength="160"
android:hint="@string/hintStatus"
android:background="@color/transparentBlue"
android:layout_width="fill_parent"
android:layout_height="100px"
android:layout_margin="5px"
android:padding="5px"
android:minLines="4"></EditText>
<Button
android:layout_height="wrap_content"
android:id="@+id/buttonUpdate"
android:text="@string/labelUpdate"
android:layout_width="fill_parent"
android:background="@drawable/buttonbg"></Button>
</LinearLayout>
當我點擊更新按鈕。它強制關閉了,我得到了以下錯誤:
Thread [<1> main] (Suspended (exception TwitterException))
URLConnectionHttpClient.post(String, Map, boolean) line: 279
Twitter.updateStatus(String, Number) line: 2471
Twitter.updateStatus(String) line: 2411
Twitter.setStatus(String) line: 2183
MyTwitter.onClick(View) line: 45
Button(View).performClick() line: 2485
View$PerformClick.run() line: 9080
ViewRoot(Handler).handleCallback(Message) line: 587
ViewRoot(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 123
ActivityThread.main(String[]) line: 3683
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 839
ZygoteInit.main(String[]) line: 597
NativeStart.main(String[]) line: not available [native method]
它有事情做與TwitterException?謝謝