[英]variable cannot be resolved in Java
我正在使用 Db、Java 和 OOP 创建一个 Android 应用程序。
这是主要活动的来源:
public class EpsoftSMSActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final MyDatabase db=new MyDatabase(getApplicationContext());
db.open(); //apriamo il db
if (db.listaParametri().getCount()==0)
{
setup_parametri();
/*final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.login);
dialog.setTitle("Login");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
//set up button
Button registra = (Button) dialog.findViewById(R.id.registra);
registra.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String username = dialog.findViewById(R.id.username).toString();
String password = dialog.findViewById(R.id.password).toString();
db.inserimentoParametri(username, password);
dialog.dismiss();
}
});
Button annulla = (Button) dialog.findViewById(R.id.annulla);
annulla.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();*/
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.layout.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.chiudi:
finish();
System.exit(0);
return true;
case R.id.setup:
setup_parametri();
return true;
case R.id.info:
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Informazioni & Credits");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
/* //set up text
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
//text.setText(R.string.lots_of_text);
//set up image view
ImageView img = (ImageView) dialog.findViewById(R.id.ImageView01);
img.setImageResource(R.drawable.ic_launcher);*/
//set up button
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
public void setup_parametri()
{
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.login);
dialog.setTitle("Login");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, check them all out!
//set up button
Button registra = (Button) dialog.findViewById(R.id.registra);
registra.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String username = dialog.findViewById(R.id.username).toString();
String password = dialog.findViewById(R.id.password).toString();
db.inserimentoParametri(username, password);
dialog.dismiss();
}
});
Button annulla = (Button) dialog.findViewById(R.id.annulla);
annulla.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
}
在名为“setup_parametri”的最新函数中,我尝试调用“db.inserimentoParametri”,但 Eclipse 告诉我:
“无法解析数据库”
db 是在 oncreate 中定义的。
这是怎么回事?
db
是在onCreate()
声明的局部变量。 它对任何其他方法都是不可见的。 您可能希望将其设为 Activity 类的实例字段,这意味着该类的所有方法都可以使用它。
在onCreate()
外部声明该字段,但在内部对其进行初始化。 像这样:
private MyDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
....
db = new MyDatabase(getApplicationContext());
您已在onCreate()
setup_parametri()
db 声明为变量,而未将其传递给setup_parametri()
您需要将其设为类变量:
public class EpsoftSMSActivity extends Activity {
/** Called when the activity is first created. */
final MyDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new MyDatabase(getApplicationContext());
db.open(); //apriamo il db
或将setup_parametri()
的签名更改为setup_parametri(MyDatabase db)
当您的 OnClickerListener 事件处理程序运行时,DB 变量不在范围内。
将 db 定义移到 onCreate 方法的范围之外,即使其成为成员变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.