我在我的应用中使用Twitter4J
库。我收到twitter
个Feed,我想将它们设置为listview
。如果我使用statuses.size()
来获取金额,那就是成功,我会打印20个Feed。但是,如果我想将数据设置为listview
,则会强制关闭。
public class MainActivity extends Activity {
List<Status> statuses = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView i = (ListView) findViewById(R.id.listView1);
final ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("xxxxxxx")
.setOAuthConsumerSecret("xxxxxxxx")
.setOAuthAccessToken("xxxxxxxx")
.setOAuthAccessTokenSecret("xxxxxxxxxx");
new Thread(new Runnable() {
@Override
public void run() {
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
try {
String user;
user = "xxxxxxx";
statuses = twitter.getUserTimeline(user);
Log.i("Status Count", statuses.size() + " Feeds");
statuses.toArray();
} catch (TwitterException te) {
te.printStackTrace();
}
}
}).start();
}
}
现在该应用有效,A layout
显示为空白(因为我没有向listview
插入数据,我得到11-10 14:53:46.269: I/Status Count(16935): 20 Feeds
,这意味着.size有效。)
i.setAdapter(new ArrayAdapter<Status>(MainActivity.this,
android.R.layout.simple_list_item_1, statuses));
但是,如果我在}).start();
之后添加此内容,eclipse
中没有错误,但应用程序强制在手机中关闭。
logcat
:
11-10 14:55:39.749: E/AndroidRuntime(18153): FATAL EXCEPTION: main
11-10 14:55:39.749: E/AndroidRuntime(18153): java.lang.RuntimeException: Unable to start activity ComponentInfo{xxxxxxxxx/xxxxxxxx.MainActivity}: java.lang.NullPointerException
11-10 14:55:39.749: E/AndroidRuntime(18153): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
11-10 14:55:39.749: E/AndroidRuntime(18153): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
11-10 14:55:39.749: E/AndroidRuntime(18153): at android.app.ActivityThread.access$600(ActivityThread.java:153)
11-10 14:55:39.749: E/AndroidRuntime(18153): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
11-10 14:55:39.749: E/AndroidRuntime(18153): at android.os.Handler.dispatchMessage(Handler.java:99)
11-10 14:55:39.749: E/AndroidRuntime(18153): at android.os.Looper.loop(Looper.java:137)
11-10 14:55:39.749: E/AndroidRuntime(18153): at android.app.ActivityThread.main(ActivityThread.java:5227)
11-10 14:55:39.749: E/AndroidRuntime(18153): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 14:55:39.749: E/AndroidRuntime(18153): at java.lang.reflect.Method.invoke(Method.java:511)
11-10 14:55:39.749: E/AndroidRuntime(18153): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-10 14:55:39.749: E/AndroidRuntime(18153): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-10 14:55:39.749: E/AndroidRuntime(18153): at dalvik.system.NativeStart.main(Native Method)
11-10 14:55:39.749: E/AndroidRuntime(18153): Caused by: java.lang.NullPointerException
11-10 14:55:39.749: E/AndroidRuntime(18153): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
11-10 14:55:39.749: E/AndroidRuntime(18153): at android.widget.ListView.setAdapter(ListView.java:462)
11-10 14:55:39.749: E/AndroidRuntime(18153): at xxxxxxxxxx.MainActivity.onCreate(MainActivity.java:63)
11-10 14:55:39.749: E/AndroidRuntime(18153): at android.app.Activity.performCreate(Activity.java:5104)
11-10 14:55:39.749: E/AndroidRuntime(18153): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-10 14:55:39.749: E/AndroidRuntime(18153): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
11-10 14:55:39.749: E/AndroidRuntime(18153): ... 11 more