0

Twitter4J在我的应用程序中使用库。我得到了twitter提要,我想将它们设置为listview. 如果我statuses.size()用来获取他们的数量,它是成功的,我会打印 20 个提要。但是,如果我想将数据设置为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();


        }
    }

现在应用程序正常工作, Alayout显示为空白(因为我没有将数据插入到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
4

0 回答 0