窃听 Twitter Android 应用程序。不会崩溃,但不会打印任何内容。请帮忙!

发布于 2024-11-15 13:58:33 字数 7952 浏览 3 评论 0原文

我的 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

请帮忙!预先感谢您的回答! :)

My android app is supposed to go through twitter using twitter4j libraries, and return a bunch of tweets with the keyword you search. The app doesn't crash like it used to, but it is also bugged somehow to where it doesn't print out anything.

Here is the code:

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;
        }
    };
}

Here is the AndroidMnifest.xml file's contents just in case you need it:

<?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>

And here is the 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

Please help! Thank you in advance for your answers! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你在看孤独的风景 2024-11-22 13:58:33

您不应该只捕获 NullPointerExceptionsTwitterExceptions,然后在 catch 块中不执行任何操作。很可能您的代码失败了,而您所做的只是阻止报告原因。删除捕获并抑制这些异常的 try/catch 块,然后找出导致抛出异常的原因。我猜想,一旦解决了这些异常的根本原因,事情就可能开始起作用。

You shouldn't just catch NullPointerExceptions and TwitterExceptions then do nothing in your catch block. The chances are that your code is failing and all you are effectively doing is preventing the cause from being reported. Remove your try/catch blocks which are catching and suppressing these exceptions and then work out what is causing them to be thrown. I would guess that once you fix your the root causes of these exceptions then things may start working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文