IllegalStateException:当我试图运行我的应用程序时onCreate()之前的活动不可用的系统服务

时间:2016-05-01 20:52:10

标签: android android-studio

我是Android新手,我尝试制作应用程序,将手机位置保存在数据库中,并在ListView中显示位置。 但是当我运行应用程序时,我有这个例外:java.lang.IllegalStateException: System services not available to Activities before onCreate()

我参考了这一行:

layoutInflater = LayoutInflater.from(context);

此行位于MyCursorAdapter类。

这是我的MainActivity代码:

public class MainActivity extends AppCompatActivity
{
    public static Cursor cursor;
    public static DBHelper dbHelper;
    public static ListView listView;
    public LocationsControl locationsControl;
    public SQLiteDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       listView = (ListView)findViewById(R.id.listView);
       this.dbHelper = new DBHelper(this.getApplicationContext());
       database = dbHelper.getReadableDatabase();
       cursor = database.query(Locations.LocationsEntry.TABLE_NAME,null,null,null,null,null,null);

       MyCursorAdapter myCursorAdapter = new MyCursorAdapter(this,cursor);
       listView.setAdapter(myCursorAdapter);

       locationsControl = new LocationsControl(this.getApplicationContext());
    }

    public void fillListView()
    {
        cursor = dbHelper.getAllTheData();
        if(cursor != null)
        {
            MyCursorAdapter myCursorAdapter = new MyCursorAdapter(this,cursor);
            myCursorAdapter.notifyDataSetChanged();
        }
    }
}

这是MyCursorAdapter类:

public class MyCursorAdapter extends CursorAdapter
{
    public static TextView longitude;
    public static TextView latitude;
    public static TextView timeAndDate;

    LayoutInflater layoutInflater;
    public MyCursorAdapter(Context context, Cursor cursor)
    {
        super(context,cursor);
        layoutInflater = LayoutInflater.from(context);//Here the problem
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent)
    {
        return     layoutInflater.inflate(R.layout.list_black_text,parent,false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor)
    {
        longitude = (TextView)view.findViewById(R.id.textView4);
        latitude = (TextView)view.findViewById(R.id.textView5);
        timeAndDate = (TextView)view.findViewById(R.id.textView6);

        longitude.setText(""+cursor.getDouble(cursor.getColumnIndex(Locations.LocationsEntry.LONGITUDE)));
        latitude.setText(""+cursor.getDouble(cursor.getColumnIndex(Locations.LocationsEntry.LATITUDE)));
        timeAndDate.setText(""+cursor.getString(cursor.getColumnIndex(Locations.LocationsEntry.DATE_AND_TIME)));
    }
}

我在网上搜索了答案,但我找到的所有答案都没有帮助我。

1 个答案:

答案 0 :(得分:0)

好吧,我发现我的代码存在问题。

以下是更改:

 public void fillListView()
 {
     cursor = dbHelper.getAllTheData();
     if(cursor != null)
     {
         myCursorAdapter.changeCursor(cursor);
         myCursorAdapter.notifyDataSetChanged();
         //myCursorAdapter = new MyCursorAdapter(this,cursor);
         //myCursorAdapter.notifyDataSetChanged();
     }
  }