尝试创建简单列表时崩溃

时间:2014-12-16 05:20:04

标签: java android xml android-listview simpleadapter

我正在尝试使用showArticles方法生成一个简单的列表,而我遇到了崩溃,我尝试了很多解决方案,但似乎我在onCreate之前调用了一些系统方法,我无法在代码中看到我的错误如下所示:

public class ArticleListActivity extends ListActivity {
    Context context;
    ListView lv;

    private static class ArticleListLoadingCallback implements Handler.Callback {
        private ListActivity activity;
        private ArticleListManager articleListManager;

        public ArticleListLoadingCallback(ListActivity activity,
                ArticleListManager articleListManager) {
            this.activity = activity;
            this.articleListManager = articleListManager;
        }

        @Override
        public boolean handleMessage(Message message) {
            throw new MethodNotYetImplementedException();
        }
    }

    private ArticleListManager articleListManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article_list);

        articleListManager = new ArticleListManager(ArticleListActivity.this,
                this.context);
        /*articleListManager.fetchArticles(new Handler(
                new ArticleListLoadingCallback(this, articleListManager)));*/

       showArticles();

    }

    @Override
    protected void onListItemClick(ListView listView, View view, int position,
            long id) {
        Article article = (Article) getListAdapter().getItem(position);
        Intent intent = new Intent(this, ArticleDetailActivity.class);
        intent.putExtra("article", article);
        this.startActivity(intent);
    }

    public void showArticles() {


        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
            "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
            "Android", "iPhone", "WindowsMobile" };

        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < values.length; ++i) {
          list.add(values[i]);
        }
        setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_article_list,list));

        ListView listView = getListView();
        listView.setTextFilterEnabled(true);
    }

}

我的XML如下:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

     <ProgressBar
        android:id="@+id/myProgress"
        android:layout_width="wrap_content"
        android:visibility="invisible"
        android:layout_height="wrap_content" />

     <ListView android:id="@android:id/list"
               android:layout_width="match_parent"
               android:layout_height="0dp"
               android:layout_weight="1"/>

 </LinearLayout>

我收到以下崩溃/错误:

> 12-15 21:03:53.581: E/AndroidRuntime(1989): FATAL EXCEPTION:
> Thread-156 12-15 21:03:53.581: E/AndroidRuntime(1989):
> java.lang.IllegalStateException: System services not available to
> Activities before onCreate() 12-15 21:03:53.581:
> E/AndroidRuntime(1989):   at
> android.app.Activity.getSystemService(Activity.java:4410) 12-15
> 21:03:53.581: E/AndroidRuntime(1989):     at
> android.widget.ArrayAdapter.init(ArrayAdapter.java:310) 12-15
> 21:03:53.581: E/AndroidRuntime(1989):     at
> android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)

如果您有任何疑问,请随时与我们联系。

0 个答案:

没有答案