我的Android应用程序应该使用twitter4j库通过twitter,并返回一堆推文与您搜索的关键字。该应用程序不会像过去那样崩溃,但它也会以某种方式被错误地打印到不打印任何内容的位置。
以下是代码:
package com.SMARTlab.twitterapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import twitter4j.*;
public class TwitterApp extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button search = (Button)findViewById(R.id.search_button);
search.setOnClickListener(search_OnClickListener);
}
public void onClick(View v)
{
switch (v.getId())
{
case R.id.search_button:
Intent i = new Intent(this, com.SMARTlab.twitterapp.TwitterApp.class);
startActivity(i);
break;
}
}
@SuppressWarnings("deprecation")
private Button.OnClickListener search_OnClickListener = new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
Twitter t = new TwitterFactory().getInstance();
EditText ed = (EditText)findViewById(R.id.search_box);
TextView txt = (TextView)findViewById(R.id.text_box);
Query q = new Query(ed.getText().toString());
QueryResult res = null;
try
{
res = t.search(q);
txt.setText("");
}
catch(TwitterException ex) {System.out.println(ex.toString());}
for (Tweet tw : res.getTweets())
{
try
{
long[] id = t.getRetweetedByIDs(tw.getId()).getIDs();
for (int x = 0; x < id.length; x++)
queryRank += 1;
} catch(TwitterException te) {}
if (evaluateRank(queryRank))
{
txt.append((String)"[" + tw.getFromUser() + "] " + tw.getText() + "\n");
}
}
}
int queryRank = 0;
private boolean evaluateRank(int rank)
{
if (rank > 10)
return true;
return false;
}
};
}
以下是AndroidMnifest.xml文件的内容,以防您需要:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.SMARTlab.twitterapp"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TwitterApp"
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>
这是LogCat:
06-13 09:35:42.036: DEBUG/AndroidRuntime(425): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
06-13 09:35:42.036: DEBUG/AndroidRuntime(425): CheckJNI is ON
06-13 09:35:42.255: DEBUG/AndroidRuntime(425): --- registering native functions ---
06-13 09:35:43.076: DEBUG/AndroidRuntime(425): Shutting down VM
06-13 09:35:43.076: DEBUG/dalvikvm(425): Debugger has detached; object registry had 1 entries
06-13 09:35:43.116: INFO/AndroidRuntime(425): NOTE: attach of thread 'Binder Thread #3' failed
06-13 09:35:43.625: DEBUG/AndroidRuntime(433): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
06-13 09:35:43.635: DEBUG/AndroidRuntime(433): CheckJNI is ON
06-13 09:35:43.855: DEBUG/AndroidRuntime(433): --- registering native functions ---
06-13 09:35:44.645: INFO/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.SMARTlab.twitterapp/.TwitterApp }
06-13 09:35:44.705: INFO/ActivityManager(59): Start proc com.SMARTlab.twitterapp for activity com.SMARTlab.twitterapp/.TwitterApp: pid=439 uid=10040 gids={}
06-13 09:35:44.755: DEBUG/AndroidRuntime(433): Shutting down VM
06-13 09:35:44.765: DEBUG/dalvikvm(433): Debugger has detached; object registry had 1 entries
06-13 09:35:44.825: INFO/AndroidRuntime(433): NOTE: attach of thread 'Binder Thread #3' failed
06-13 09:35:46.026: INFO/ActivityManager(59): Displayed activity com.SMARTlab.twitterapp/.TwitterApp: 1329 ms (total 1329 ms)
06-13 09:35:51.135: DEBUG/dalvikvm(123): GC_EXPLICIT freed 230 objects / 9920 bytes in 75ms
06-13 09:36:51.735: WARN/KeyCharacterMap(439): No keyboard for id 0
06-13 09:36:51.735: WARN/KeyCharacterMap(439): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-13 09:36:57.306: INFO/System.out(439): Permission deniedRelevant discussions can be on the Internet at:
06-13 09:36:57.306: INFO/System.out(439): http://www.google.co.jp/search?q=d35baff5 or
06-13 09:36:57.316: INFO/System.out(439): http://www.google.co.jp/search?q=08a954d5
06-13 09:36:57.316: INFO/System.out(439): TwitterException{exceptionCode=[d35baff5-08a954d5 43208640-0c814192], statusCode=-1, retryAfter=-1, rateLimitStatus=null, featureSpecificRateLimitStatus=null, version=2.2.3}
06-13 09:36:57.316: DEBUG/AndroidRuntime(439): Shutting down VM
06-13 09:36:57.326: WARN/dalvikvm(439): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-13 09:36:57.336: ERROR/AndroidRuntime(439): FATAL EXCEPTION: main
06-13 09:36:57.336: ERROR/AndroidRuntime(439): java.lang.NullPointerException
06-13 09:36:57.336: ERROR/AndroidRuntime(439): at com.SMARTlab.twitterapp.TwitterApp$1.onClick(TwitterApp.java:50)
06-13 09:36:57.336: ERROR/AndroidRuntime(439): at android.view.View.performClick(View.java:2408)
06-13 09:36:57.336: ERROR/AndroidRuntime(439): at android.view.View$PerformClick.run(View.java:8816)
06-13 09:36:57.336: ERROR/AndroidRuntime(439): at android.os.Handler.handleCallback(Handler.java:587)
06-13 09:36:57.336: ERROR/AndroidRuntime(439): at android.os.Handler.dispatchMessage(Handler.java:92)
06-13 09:36:57.336: ERROR/AndroidRuntime(439): at android.os.Looper.loop(Looper.java:123)
06-13 09:36:57.336: ERROR/AndroidRuntime(439): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-13 09:36:57.336: ERROR/AndroidRuntime(439): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 09:36:57.336: ERROR/AndroidRuntime(439): at java.lang.reflect.Method.invoke(Method.java:521)
06-13 09:36:57.336: ERROR/AndroidRuntime(439): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-13 09:36:57.336: ERROR/AndroidRuntime(439): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-13 09:36:57.336: ERROR/AndroidRuntime(439): at dalvik.system.NativeStart.main(Native Method)
06-13 09:36:57.356: WARN/ActivityManager(59): Force finishing activity com.SMARTlab.twitterapp/.TwitterApp
06-13 09:36:57.875: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{45050718 com.SMARTlab.twitterapp/.TwitterApp}
06-13 09:37:00.995: INFO/Process(439): Sending signal. PID: 439 SIG: 9
06-13 09:37:01.015: INFO/ActivityManager(59): Process com.SMARTlab.twitterapp (pid 439) has died.
06-13 09:37:01.015: INFO/WindowManager(59): WIN DEATH: Window{4503edf8 com.SMARTlab.twitterapp/com.SMARTlab.twitterapp.TwitterApp paused=false}
06-13 09:37:01.085: WARN/InputManagerService(59): Got RemoteException sending setActive(false) notification to pid 439 uid 10040
请帮忙!提前感谢您的回答! :)
答案 0 :(得分:1)
你不应该只抓住NullPointerExceptions
和TwitterExceptions
然后在你的catch区块中什么都不做。很可能你的代码失败了,你所做的一切都是防止报告原因。删除正在捕获的try/catch
块并抑制这些异常,然后找出导致它们被抛出的原因。我猜想,一旦你解决了这些异常的根本原因,那么事情可能会开始起作用。