java - Android 变量未定义

标签 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;

        }

    }
}

最佳答案

试试这个代码

您的按钮 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 的机会...

关于java - Android 变量未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24933421/

相关文章:

android - 如何在 Android 中从 Java 代码更改/重置自定义主题

android - 如何保存原始 fragment 的内容,同时去其他 fragment 并返回原始 fragment ?

java - 模拟对象单元测试遇到问题

java - 自定义 Java 核心类

java - 如何解决 OrmLite 中巨大的 byte[] 问题

java - EJB 的数据源问题

java - 如何在 SPRING REST API 中使用 SQL 查询处理 "ALL"?

iphone - Rails3 和 Devise 与 Iphone/Android/Wp7 应用程序一起工作

java - 异常 org.springframework.beans.factory.CannotLoadBeanClassException : Cannot find class

XML(非 C++)文件中的 Eclipse CDT 拼写检查