OnCreate预计在Android工作室中使用分号

时间:2016-03-01 14:06:52

标签: java android android-studio oncreate

    public class MainActivity extends AppCompatActivity {
        private TaskDBHelper helper;
        private ListAdapter listAdapter;
        @Override
        public void onCreate(Bundle savedInstanceState) {

            private void updateUI(){
                helper = new TaskDBHelper(MainActivity.this);
                SQLiteDatabase sqlDB = helper.getReadableDatabase();
                Cursor cursor = sqlDB.query(TaskContract.TABLE,
                        new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK},
                        null, null, null, null, null);

                listAdapter = new SimpleCursorAdapter(
                        this,
                        R.layout.task_view,
                        cursor,
                        new String[]{TaskContract.Columns.TASK},
                        new int[]{R.id.taskTextView},
                        0
                );
                ListView listView = (ListView) findViewById(R.id.list);
                listView.setAdapter(listAdapter);
            }
        }
}

我一直在private void updateUI()上出现一个红色标记,其中显示一条表示预期的错误消息(分号)。

2 个答案:

答案 0 :(得分:2)

试试这个:

 public class MainActivity extends AppCompatActivity {
        private TaskDBHelper helper;
        private ListAdapter listAdapter;
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                updateUI();
                ListView listView = (ListView) findViewById(R.id.list);
                listView.setAdapter(listAdapter);
            }

            private void updateUI(){
                helper = new TaskDBHelper(MainActivity.this);
                SQLiteDatabase sqlDB = helper.getReadableDatabase();
                Cursor cursor = sqlDB.query(TaskContract.TABLE,
                        new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK},
                        null, null, null, null, null);

                listAdapter = new SimpleCursorAdapter(
                        this,
                        R.layout.task_view,
                        cursor,
                        new String[]{TaskContract.Columns.TASK},
                        new int[]{R.id.taskTextView},
                        0
                );
        }
}

答案 1 :(得分:2)

在Java中,您无法在另一个方法的范围内声明方法。您只能在类的范围内声明方法。因此,声明方法updateUI()的方式无效,应移至类范围。