我是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)));
}
}
我在网上搜索答案,但是发现的所有答案都帮不了我。
好的,我发现我的代码有问题。
这是更改:
public void fillListView()
{
cursor = dbHelper.getAllTheData();
if(cursor != null)
{
myCursorAdapter.changeCursor(cursor);
myCursorAdapter.notifyDataSetChanged();
//myCursorAdapter = new MyCursorAdapter(this,cursor);
//myCursorAdapter.notifyDataSetChanged();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句