gpt4 book ai didi

java - Android 变量未定义

转载 作者:行者123 更新时间:2023-12-01 07:25:53 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com