Intent不能用于onClick android studio

时间:2016-06-22 11:23:33

标签: android android-intent this start-activity

我试图在复选框的onClick方法中使用intent,但是我得到的错误是方法startActivity未定义,并且构造函数Intent(new View.OnClickListener等)也是未定义的。这是我的代码:

com.tutorials.hp.tanding.Main.onClick(Main.java:24)

时出现

错误

public class Main extends Activity implements View.OnClickListener{

    private TeamHandler handler;
    private List<Team> teams;
    ListView lv;

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Main.this, NewTeam.class);
        startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        handler = new TeamHandler(getApplicationContext());

        Button btn_add_new = (Button) findViewById(R.id.btn_add_team);
        btn_add_new.setOnClickListener(this);

        lv = (ListView) findViewById(R.id.lv_team_list);

        loadTeamData();

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {
                Intent intent = new Intent(Main.this, TeamDetails.class);
                intent.putExtra("id", teams.get(position).getID());
                intent.putExtra("name", teams.get(position).getName());
                intent.putExtra("phone", teams.get(position).getPhoneNumber());
                intent.putExtra("email", teams.get(position).getEmail());
                intent.putExtra("address", teams.get(position).getPostalAddress());
                intent.putExtra("photograph", teams.get(position).getPhotograph());
                startActivity(intent);

            }

        });

    }



    private void loadTeamData(){
        // Code for loading team list in ListView
        // Reading all teams
        teams = handler.readAllTeams();

        // Initialize Custom Adapter
        CustomAdapter adapter = new CustomAdapter(this, teams);

        // Set Adapter to ListView
        lv.setAdapter(adapter);

        // See the log int LogCat
        for(Team c : teams){
            String record = "ID=" + c.getID() + " | Name=" + c.getName() + " | " + c.getPhoneNumber();
            Log.d("Record",record);
        }

    }
}

0 个答案:

没有答案