Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()

报错信息:Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()

报错原因:由于在类中直接实例化对象

具体错误实例:直接在实体类中初始化ArrayAdapter 

public class MainActivity extends Activity implements SearchView.OnQueryTextListener {
    // 自动完成的列表
    private final String[] mStrings = { "aaaaa", "bbbbbb", "cccccc", "ddddddd" };
    //自适应
    public ArrayAdapter adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mStrings);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      
    }
}

正确解决方法:

  • 第一步:在类中定义:ArrayAdapter<String> adapter;
  • 第二步:在onCreate方法中将这个ArrayAdapter实例化

修改方法:

public class MainActivity extends Activity implements SearchView.OnQueryTextListener {
    // 自动完成的列表
    private final String[] mStrings = { "aaaaa", "bbbbbb", "cccccc", "ddddddd" };
    //自适应
    public ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mStrings);
 } 
}

posted on 2019-12-25 14:39  迷途行者  阅读(1369)  评论(0编辑  收藏  举报

导航