我正在开发我的 android 项目,但遇到了这个运行时错误:不幸的是,Example2 已停止。我想做的只是使用 twitter4j (twitter4j-core-3.0.3.jar) 显示 twitter 提要,但这段代码有一个例外:
tweets = mTwitter.search(new Query(queryString)).getTweets();
我用 java 应用程序在 NetBean 上对此进行了测试,它非常好。显示所有推特,我认为用户身份验证没有问题。我尝试按照本教程进行操作:http: //nosemaj.org/android-twitter
它似乎适用于某些人,但不适用于我:(
这是代码:
公共类 MainActivity 扩展 Activity {
Context mContext;
Twitter mTwitter;
ListView mListView;
private static final String TWITTER_CONSUMER_KEY = "xx";
private static final String TWITTER_SECRET_KEY = "xxx";
private static final String TWITTER_ACCESS_TOKEN = "xx";
private static final String TWITTER_ACCESS_TOKEN_SECRET = "xx";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listview);
mContext = getApplicationContext();
mTwitter = getTwitter();
}
protected void onResume()
{
super.onResume();
new AsyncTask<Void,Void,Void>(){
protected Void doInBackground(Void... args){
showTweetsAbout("happy");
return null;
}
}.execute();
}
private Twitter getTwitter() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
cb.setOAuthConsumerSecret(TWITTER_SECRET_KEY);
cb.setOAuthAccessToken(TWITTER_ACCESS_TOKEN);
cb.setOAuthAccessTokenSecret(TWITTER_ACCESS_TOKEN_SECRET);
return new TwitterFactory(cb.build()).getInstance();
}
private void showTweetsAbout(String queryString) {
List<Status> tweets = new ArrayList<Status>();
ArrayList<String> tweetTexts = new ArrayList<String>();
try {
tweets = mTwitter.search(new Query(queryString)).getTweets()//if I comment this line, the code works fine
// for (Status t : tweets) {
tweetTexts.add("asdadsadasd\n\n");
//}
} catch (Exception e) {
tweetTexts.add("Twitter query failed: " + e.toString());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, tweetTexts);
mListView.setAdapter(adapter);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.example2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
这是logCat
07-01 07:39:24.113: E/AndroidRuntime(3979): at android.view.View.setFlags(View.java:8412)
07-01 07:39:24.113: E/AndroidRuntime(3979): at android.view.View.setFocusableInTouchMode(View.java:5790)
07-01 07:39:24.113: E/AndroidRuntime(3979): at android.widget.AdapterView.checkFocus(AdapterView.java:717)
07-01 07:39:24.113: E/AndroidRuntime(3979): at android.widget.ListView.setAdapter(ListView.java:463)
07-01 07:39:24.113: E/AndroidRuntime(3979): at com.example.example2.MainActivity.showTweetsAbout(MainActivity.java:94)
07-01 07:39:24.113: E/AndroidRuntime(3979): at com.example.example2.MainActivity.access$0(MainActivity.java:78)
07-01 07:39:24.113: E/AndroidRuntime(3979): at com.example.example2.MainActivity$1.doInBackground(MainActivity.java:52)
07-01 07:39:24.113: E/AndroidRuntime(3979): at com.example.example2.MainActivity$1.doInBackground(MainActivity.java:1)
07-01 07:39:24.113: E/AndroidRuntime(3979): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-01 07:39:24.113: E/AndroidRuntime(3979): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-01 07:39:24.113: E/AndroidRuntime(3979): ... 4 more
07-01 07:42:19.853: E/AndroidRuntime(4049): FATAL EXCEPTION: AsyncTask #1
07-01 07:42:19.853: E/AndroidRuntime(4049): java.lang.RuntimeException: An error occured while executing doInBackground()
07-01 07:42:19.853: E/AndroidRuntime(4049): at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-01 07:42:19.853: E/AndroidRuntime(4049): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
07-01 07:42:19.853: E/AndroidRuntime(4049): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
07-01 07:42:19.853: E/AndroidRuntime(4049): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
07-01 07:42:19.853: E/AndroidRuntime(4049): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-01 07:42:19.853: E/AndroidRuntime(4049): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-01 07:42:19.853: E/AndroidRuntime(4049): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-01 07:42:19.853: E/AndroidRuntime(4049): at java.lang.Thread.run(Thread.java:856)
07-01 07:42:19.853: E/AndroidRuntime(4049): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-01 07:42:19.853: E/AndroidRuntime(4049): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
07-01 07:42:19.853: E/AndroidRuntime(4049): at android.view.ViewRootImpl.focusableViewAvailable(ViewRootImpl.java:2588)
07-01 07:42:19.853: E/AndroidRuntime(4049): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
07-01 07:42:19.853: E/AndroidRuntime(4049): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
07-01 07:42:19.853: E/AndroidRuntime(4049): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
07-01 07:42:19.853: E/AndroidRuntime(4049): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
07-01 07:42:19.853: E/AndroidRuntime(4049): at android.view.View.setFlags(View.java:8412)
07-01 07:42:19.853: E/AndroidRuntime(4049): at android.view.View.setFocusableInTouchMode(View.java:5790)
07-01 07:42:19.853: E/AndroidRuntime(4049): at android.widget.AdapterView.checkFocus(AdapterView.java:717)
07-01 07:42:19.853: E/AndroidRuntime(4049): at android.widget.ListView.setAdapter(ListView.java:463)
07-01 07:42:19.853: E/AndroidRuntime(4049): at com.example.example2.MainActivity.showTweetsAbout(MainActivity.java:94)
07-01 07:42:19.853: E/AndroidRuntime(4049): at com.example.example2.MainActivity.access$0(MainActivity.java:78)
07-01 07:42:19.853: E/AndroidRuntime(4049): at com.example.example2.MainActivity$1.doInBackground(MainActivity.java:52)
07-01 07:42:19.853: E/AndroidRuntime(4049): at com.example.example2.MainActivity$1.doInBackground(MainActivity.java:1)
07-01 07:42:19.853: E/AndroidRuntime(4049): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-01 07:42:19.853: E/AndroidRuntime(4049): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-01 07:42:19.853: E/AndroidRuntime(4049): ... 4 more
非常感谢您的反馈!