Android变量未定义

时间:2014-07-24 12:15:09

标签: java android eclipse variables

Eclipse一直告诉我,advancedButton“无法解析或不是一个字段”,即使我已经定义它就像我班上的所有其他变量一样。即使我尝试用另一个变量替换它,它也会给我同样的错误。有关为什么会发生这种情况的任何线索?

    public void onClick(View w) {

        switch(w.getId()){
        case R.id.advancedButton:

            Intent a = new Intent(Registration.this, RegistrationAdvanced.class);
            startActivity(a);
            break;

        }

整个代码

package com.example.testing;
import java.io.Console;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.os.Bundle;  
import android.app.Activity;  
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;  
import android.view.MenuItem;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;  
import android.widget.EditText;

public class Registration extends Activity implements OnClickListener{

    Button dobButton, countryButton, advancedButton;

    ArrayList<String> dobList = new ArrayList<String>();
    ArrayList<String> countryList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration);

        Initialize();
        PopulateDate();
        PopulateCountries();
    }

    public void Initialize(){

        dobButton = (Button) findViewById(R.id.dob_button);
        dobButton.setTag("1");
        dobButton.setOnClickListener(this);

        countryButton = (Button) findViewById(R.id.country_button);
        countryButton.setTag("2");
        countryButton.setOnClickListener(this);

        advancedButton = (Button) findViewById(R.id.advanced_button);
        advancedButton.setOnClickListener(this);
    }

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch(arg0.getId()){
        case R.id.advancedButton:

            Intent a = new Intent(Registration.this, RegistrationAdvanced.class);

            startActivity(a);
            break;

        }

    }
}

1 个答案:

答案 0 :(得分:7)

试试这段代码

您的按钮ID为R.id.advanced_button,并且您已写入R.id.advancedButton

   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch(arg0.getId()){
    case R.id.advanced_button:

        Intent a = new Intent(Registration.this, RegistrationAdvanced.class);

        startActivity(a);
        break;

    }

}

修改

我遵循像viewType_layoutName_ViewName这样的命名惯例所以我在编写R.id.XXXX

时并不这么说

例如,在您的情况下,我的按钮ID会像R.id.btn_registration_advance这样减少混淆ID的机会......