错误“令牌上的语法错误,错放的构造(s),此令牌MainActivity.java之后的VariableDeclaratorId”

时间:2012-09-02 19:30:06

标签: android

package com.example.temp_application;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    final ListView myLV = (ListView)findViewById(R.id.myLV);
    final EditText myET = (EditText)findViewById(R.id.myET);

    final ArrayList<String> todoList = new ArrayList<String>();
    final ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoList);

    myLV.setAdapter(aa);
}

我收到错误:

  

令牌上的语法错误,错放的构造,令牌“aa”上的语法错误,此令牌之后的VariableDeclaratorId MainActivity.java

在这一行

myLV.setAdapter(aa);

3 个答案:

答案 0 :(得分:5)

将此代码放在的onCreate()方法中:

final ListView myLV = (ListView)findViewById(R.id.myLV);
final EditText myET = (EditText)findViewById(R.id.myET);

final ArrayList<String> todoList = new ArrayList<String>();
final ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoList);

myLV.setAdapter(aa);

要明确:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // The above code goes here
}

答案 1 :(得分:0)

变量的初始化应该在你的案例中的onCreate()方法中完成

答案 2 :(得分:0)

有问题的行不在函数体中:

myLV.setAdapter(aa);

您可能希望将该行和变量声明移到onCreate()函数中。