无法解析
我是人,我有一个很大的问题:我正在用Db创建我的第一个Android应用程序,这是我第二周使用java和OOP。无法解析
这是主要活动的来源:
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中定义。
Whatsup? TNX。
您已经声明DB作为内onCreate()
的变量,而不将它传递给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)
db
是在onCreate()
内部声明的局部变量。它对任何其他方法都是不可见的。您可能希望将其设置为Activity类的实例字段,这意味着它将适用于该类的所有方法。
声明onCreate()
以外的字段,但在里面进行初始化。像这样:
private MyDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
....
db = new MyDatabase(getApplicationContext());
当您的OnClickerListener事件处理程序运行时,DB变量不在作用域中。
将db定义移动到onCreate方法的范围之外,即使其成为成员变量。
我已经尝试过(在我实际上对OOP无知)在oncreate之前声明db。结果:在编译时没有错误,但在执行时发生错误。 –
我试过了(在我实际上对OOP无知的情况下)在oncreate之前声明db。结果:在编译时没有错误,但在执行时发生错误。 –
什么是错误?你是如何申报的? –
我在第一个@override之前以相同的模式声明它。 该错误是本 一月6日至9日:27:16.252:E/AndroidRuntime(278):显示java.lang.NullPointerException 一月6日至9日:27:16.252:致E/AndroidRuntime(278):\t在android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100) –