我是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)));
}
}
我在网上搜索了答案,但我找到的所有答案都没有帮助我。
答案 0 :(得分:0)
以下是更改:
public void fillListView()
{
cursor = dbHelper.getAllTheData();
if(cursor != null)
{
myCursorAdapter.changeCursor(cursor);
myCursorAdapter.notifyDataSetChanged();
//myCursorAdapter = new MyCursorAdapter(this,cursor);
//myCursorAdapter.notifyDataSetChanged();
}
}