我在 Android 中使用以下代码使用 Twitter4J 获取 Twitter 时间线。但是我在 Activity 中得到 Nullpointer 异常?任何想法 ?您还可以建议任何其他工作教程以在无需身份验证的情况下在 Android 中获取 Twitter Feed。尝试了很多,但几乎所有的都失败了。
public class TwitterTimelineActivity extends Activity {
String[] args;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tweet);
Twitter twitter = new TwitterFactory().getInstance();
try {
List<Status> statuses;
String user;
if (args.length == 1) {
user = args[0];
statuses = twitter.getUserTimeline(user);
} else {
//user = twitter.verifyCredentials().getScreenName();
user = "doubletree";
statuses = twitter.getUserTimeline();
}
System.out.println("Showing @" + user + "'s user timeline.");
for (Status status : statuses) {
System.out.println("Timeline : " + "@" + status.getUser().getScreenName()
+ " - " + status.getText());
}
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to get timeline: " + te.getMessage());
System.exit(-1);
}
}
}
低于例外:
07-18 13:31:17.525: E/AndroidRuntime(29810): FATAL EXCEPTION: main
07-18 13:31:17.525: E/AndroidRuntime(29810): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.itcuties.multicategoryrssreader/com.itcuties.multicategoryrssreader.TwitterTimelineActivity}: java.lang.NullPointerException
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1941)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:705)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.widget.TabHost.setCurrentTab(TabHost.java:369)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:560)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.view.View.performClick(View.java:4223)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.view.View$PerformClick.run(View.java:17275)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.os.Handler.handleCallback(Handler.java:615)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.os.Looper.loop(Looper.java:137)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-18 13:31:17.525: E/AndroidRuntime(29810): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 13:31:17.525: E/AndroidRuntime(29810): at java.lang.reflect.Method.invoke(Method.java:511)
07-18 13:31:17.525: E/AndroidRuntime(29810): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
07-18 13:31:17.525: E/AndroidRuntime(29810): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
07-18 13:31:17.525: E/AndroidRuntime(29810): at dalvik.system.NativeStart.main(Native Method)
07-18 13:31:17.525: E/AndroidRuntime(29810): Caused by: java.lang.NullPointerException
07-18 13:31:17.525: E/AndroidRuntime(29810): at com.itcuties.multicategoryrssreader.TwitterTimelineActivity.onCreate(TwitterTimelineActivity.java:24)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.app.Activity.performCreate(Activity.java:5206)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-18 13:31:17.525: E/AndroidRuntime(29810): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
07-18 13:31:17.525: E/AndroidRuntime(29810): ... 18 more