运行Android应用程序时出现问题,出现此错误:
无法在父级或祖先上下文中找到AddContact(View)方法。
我无法看到问题,因为我没有写“@Override”并正确拼写方法名称。
这里涉及3个java类:UserDbHelper
,UserContact
和NewContactActivity
。
UserDbHelper.java
public class UserDbHelper extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "USERINFO.DB";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_QUERY =
"CREATE TABLE "+ UserContact.NewUserInfo.TABLE_NAME+"("+UserContact.NewUserInfo.USER_NAME+" TEXT,"+
UserContact.NewUserInfo.USER_MOB+" TEXT," + UserContact.NewUserInfo.USER_EMAIL+" TEXT);";
public UserDbHelper(Context context) //constructor
{
//superclass method constructor
//pass three arguement - context object, database name, db version
super(context,DATABASE_NAME,null,DATABASE_VERSION);
Log.e("DATABASE OPERATIONS","Database created/opened...");//log message
}
@Override
public void onCreate (SQLiteDatabase db)
{
//create table in this function
//database created for the 1st time
//if already exist, won't be calling this function
db.execSQL(CREATE_QUERY);
Log.e("DATABASE OPERATIONS","Table created...");//log message
}
public void addInformation(String name, String mob, String email,SQLiteDatabase db)
{
ContentValues contentValues = new ContentValues();
contentValues.put(UserContact.NewUserInfo.USER_NAME, name);
contentValues.put(UserContact.NewUserInfo.USER_MOB, mob);
contentValues.put(UserContact.NewUserInfo.USER_EMAIL, email);
db.insert(UserContact.NewUserInfo.TABLE_NAME, null, contentValues);
Log.e("DATABASE OPERATIONS","One row inserted...");
}
NewContactActivity.java
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class NewContactActivity extends Activity
{
EditText ContactName, ContactMobile, ContactEmail;
Context context = this;
UserDbHelper userDbHelper;
SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContactName = (EditText)findViewById(R.id.user_contact_name);
ContactMobile = (EditText)findViewById(R.id.user_contact_no);
ContactEmail = (EditText)findViewById(R.id.user_email_add);
}
public void addContact(View v)
{
String name = ContactName.getText().toString();
String mob = ContactMobile.getText().toString();
String email = ContactEmail.getText().toString();
userDbHelper = new UserDbHelper(context);
sqLiteDatabase = userDbHelper.getWritableDatabase();//get writing object
userDbHelper.addInformation(name, mob, email, sqLiteDatabase);
Toast.makeText(getBaseContext(), "Data Saved", Toast.LENGTH_LONG).show();
userDbHelper.close();
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.sqlitedatabaseex.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/contact_name"
android:textSize="25sp"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
/>
<EditText
android:id="@+id/user_contact_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/contact_name_edit"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/contact_no"
android:textSize="25sp"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
/>
<EditText
android:id="@+id/user_contact_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/contact_no_edit"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/email_add"
android:textSize="25sp"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
/>
<EditText
android:id="@+id/user_email_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/email_add_edit"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:onClick="addContact"
/>
</LinearLayout>
UserContact.java
public class UserContact {
public static abstract class NewUserInfo
{
public static final String USER_NAME = "user_name"; //column name
public static final String USER_MOB = "user_mob";
public static final String USER_EMAIL = "user_email";
public static final String TABLE_NAME = "user_info";//table name
}
}
以下是Eclipse中显示的错误: