我正在做一个你写一个词的应用程序,一个新的活动打开,然后所有的推文都应该出现,但我得到了一个错误。
我printTweets
在新活动的主要内容中调用该方法:
不要担心我所有的凭据都有效,我已经在 java 应用程序(不是 android)上尝试了代码,它运行良好,我得到了结果。
这是我的代码:
public void printTweets(String q) {
LinearLayout layout = new LinearLayout(this);
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("xxxxx")
.setOAuthConsumerSecret("xxxxxx")
.setOAuthAccessToken("xxxxxx")
.setOAuthAccessTokenSecret("xxxxx");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
Query query = new Query(q);
QueryResult result = null;
do{
try {
result = twitter.search(query);
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
TextView tv=new TextView(getApplicationContext());
tv.setText("@" + tweet.getUser().getScreenName() + ":" + tweet.getText());
layout.addView(tv);
}
} while ((query = result.nextQuery()) != null);
}
这是我的错误:
11-15 01:07:30.890: E/AndroidRuntime(12926): 致命异常: main 11-15 01:07:30.890: E/AndroidRuntime(12926): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. example.sentimentmining/com.example.sentimentmining.DisplayResults}:android.os.NetworkOnMainThreadException 11-15 01:07:30.890:E/AndroidRuntime(12926):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970) 11 -15 01:07:30.890: E/AndroidRuntime(12926): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 android.app .ActivityThread.access$600(ActivityThread.java:128) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161) 11-15 01: 07:30.890:E/AndroidRuntime(12926):在 android.os.Handler。dispatchMessage(Handler.java:99) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 android.os.Looper.loop(Looper.java:137) 11-15 01:07:30.890: E/ AndroidRuntime(12926): 在 android.app.ActivityThread.main(ActivityThread.java:4514) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 java.lang.reflect.Method.invokeNative(Native Method) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 java.lang.reflect.Method.invoke(Method.java:511) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 11-15 01:07:30.890: E/AndroidRuntime(12926): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:557) 11-15 01:07:30.890: E/AndroidRuntime(12926): at dalvik.system.NativeStart.main(Native Method) 11-15 01:07:30.890: E/AndroidRuntime(12926): Caused作者:android.os。NetworkOnMainThreadException 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 11-15 01:07:30.890: E/AndroidRuntime(12926):在 java.net.InetAddress.lookupHostByName(InetAddress.java:391) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 java.net.InetAddress.getAllByNameImpl(InetAddress.java:242) 11-15 01 :07:30.890: E/AndroidRuntime(12926): at java.net.InetAddress.getAllByName(InetAddress.java:220) 11-15 01:07:30.890: E/AndroidRuntime(12926): at libcore.net.http。 HttpConnection.(HttpConnection.java:71) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 libcore.net.http.HttpConnection.(HttpConnection.java:50) 11-15 01:07:30.890: E/AndroidRuntime(12926):在 libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351) 11-15 01:07:30。890: E/AndroidRuntime(12926): 在 libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 libcore.net.http.HttpConnection .connect(HttpConnection.java:128) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308) 11-15 01:07:30.890 : E/AndroidRuntime(12926): 在 libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 libcore.net.http。 HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282) 11-15 01: 07:30.890:E/AndroidRuntime(12926):在 libcore.net.http.HttpEngine。sendRequest(HttpEngine.java:232) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 libcore.net.http.HttpsURLConnectionImpl.getResponseCode (HttpsURLConnectionImpl.java:133) 11-15 01:07:30.890: E/AndroidRuntime(12926): at twitter4j.internal.http.HttpResponseImpl.(HttpResponseImpl.java:34) 11-15 01:07:30.890: E/ AndroidRuntime(12926): 在 twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:141) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper .java:61) 11-15 01:07:30.890:E/AndroidRuntime(12926): 在 twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:81) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 twitter4j.TwitterImpl.get(TwitterImpl.java :1929) 11-15 01:07:30.890: E/AndroidRuntime(12926): at twitter4j.TwitterImpl.search(TwitterImpl.java:306) 11-15 01:07:30.890: E/AndroidRuntime(12926): at com .example.sentimentmining.DisplayResults.printTweets(DisplayResults.java:83) 11-15 01:07:30.890: E/AndroidRuntime(12926): at com.example.sentimentmining.DisplayResults.onCreate(DisplayResults.java:34) 11- 15 01:07:30.890: E/AndroidRuntime(12926): 在 android.app.Activity.performCreate(Activity.java:4465) 11-15 01:07:30.890: E/AndroidRuntime(12926): 在 android.app。 Instrumentation.callActivityOnCreate(Instrumentation.java:1053) 11-15 01:07:30.890: E/AndroidRuntime(12926):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934) 11-15 01:07:30.890: E/AndroidRuntime(12926): ... 11 更多
如果你还需要什么,请告诉我,我会给你