无法实例化Class,也没有空构造函数错误

时间:2014-05-20 18:34:21

标签: java android sqlite

我正在创建一个应用程序,我已经创建了一个类设置email.java,我在其中保存了用户在sqlite数据库中输入的值。但是这个类无法加载,它说的是:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.astrada/com.example.astrada.Setupemail}: 
java.lang.InstantiationException: can't instantiate class com.example.astrada.Setupemail; no empty constructor

这是我的代码:

package com.example.astrada;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.example.astrada.R;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Setupemail extends Activity implements OnClickListener {
    EditText editEmail, editPhon1, editPhon2, editphone3, editphone4, uname,
            useraddress;
    TextView TitleInfo;
    Button save;
    SqlOpenHelper mHelper;
    Cursor mCursor;
    SimpleCursorAdapter mAdapter;
    private final Context context;

    private SqlOpenHelper DBHelper;
    private SQLiteDatabase mDb;
    private static final String DATABASE_NAME = "databaseofuser.db";
    private static final String DATABASE_TABLE = "usertable";
    private static final int DATABASE_VERSION = 1;
    public static final String EMAIL_COLUMN = "email";
    public static final String PHONE1_COLUMN = "phon1";
    public static final String PHONE2_COLUMN = "phon2";
    public static final String PHONE3_COLUMN = "phon3";
    public static final String PHONE4_COLUMN = "phon4";
    public static final String UNAME_COLUMN = "phon4";
    public static final String UADDR_COLUMN = "phon4";

    private static final String DATABASE_CREATE = "create table "
            + DATABASE_TABLE + " (" + EMAIL_COLUMN + "," + PHONE1_COLUMN + ","
            + PHONE2_COLUMN + "," + PHONE3_COLUMN + "," + PHONE4_COLUMN + ","
            + UNAME_COLUMN + "," + UADDR_COLUMN + ");";

    public Setupemail(Context ctx) {
        this.context = ctx;
        DBHelper = new SqlOpenHelper(context);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setupemail);
        editEmail = (EditText) this.findViewById(R.id.editEmail);
        editPhon1 = (EditText) this.findViewById(R.id.editPhon1);
        editPhon2 = (EditText) this.findViewById(R.id.editPhon2);
        editphone3 = (EditText) this.findViewById(R.id.editPhon3);
        editphone4 = (EditText) this.findViewById(R.id.editPhon3);
        uname = (EditText) this.findViewById(R.id.nameofuser);
        useraddress = (EditText) this.findViewById(R.id.addressofuser);
        editPhon1.append("+91");
        editPhon2.append("+91");
        editphone3.append("+91");
        editphone4.append("+91");
        mHelper = new SqlOpenHelper(this);

        save = (Button) this.findViewById(R.id.btnSaveData);

        save.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String email, phon1, phon2, phon3, phon4;
        boolean isEntryValid, isPhon1Valid, isPhone2Valid;
        if (v == save) {
            email = editEmail.getText().toString();
            phon1 = editPhon1.getText().toString();
            phon2 = editPhon2.getText().toString();
            phon3 = editphone3.getText().toString();
            phon4 = editphone4.getText().toString();

            if (isEmailIdValid(email) && isPhoneValid(phon1)
                    && isPhoneValid(phon2)) {
                if (phon1.equals(phon2) || phon1.equals(phon3)
                        || phon1.equals(phon4) || phon2.equals(phon3)
                        || phon2.equals(phon4) || phon3.equals(phon4)) {
                    Toast.makeText(getApplicationContext(),
                            " Can't enter same numbers!!", Toast.LENGTH_LONG)
                            .show();
                } else {
                    // /Saveinto DB

                    ContentValues cv = new ContentValues();
                    cv.put(EMAIL_COLUMN, editEmail.getText().toString());
                    cv.put(PHONE1_COLUMN, editPhon1.getText().toString());
                    cv.put(PHONE2_COLUMN, editPhon2.getText().toString());
                    cv.put(PHONE3_COLUMN, editphone3.getText().toString());
                    cv.put(UNAME_COLUMN, uname.getText().toString());
                    cv.put(UADDR_COLUMN, useraddress.getText().toString());
                    mDb.insert(DATABASE_TABLE, null, cv);
                    mCursor.requery();
                    mAdapter.notifyDataSetChanged();

                    Toast.makeText(getApplicationContext(), "Good Job!!",
                            Toast.LENGTH_LONG).show();
                }
            } else {
                String x = "Invalid Entry!!\nProvide Information Properly..";
                Toast.makeText(getApplicationContext(), x, Toast.LENGTH_LONG)
                        .show();
                editEmail.setText("");
            }

        }
        // TODO Auto-generated method stub

    }

    private boolean isPhoneValid(String phon) {
        final Pattern pattern = Pattern.compile(
                "^([\\+](91))?([7-9]{1})([0-9]{9})$", Pattern.CASE_INSENSITIVE
                        | Pattern.MULTILINE);
        Matcher match = pattern.matcher(phon);

        if (match.matches()) {
            return true;
        } else {
            return false;
        }

    }

    private boolean isEmailIdValid(String Email) {
        boolean isValid = false;
        String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
        CharSequence inputStr = Email;
        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;

    }

    public class SqlOpenHelper extends SQLiteOpenHelper {
        public SqlOpenHelper() {
            super(context, null, null, 0);
        }

        public SqlOpenHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION, null);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(DATABASE_CREATE);

        }

        public SQLiteOpenHelper open() throws SQLException {
            mDb = DBHelper.getWritableDatabase();
            return this;
        }

        public void colse() {
            DBHelper.close();
        }

        public long insertContact(String email, String phon1, String phon2,
                String phon3, String phon4, String name, String address) {
            ContentValues initialValues = new ContentValues();
            initialValues.put(EMAIL_COLUMN, email);
            initialValues.put(PHONE1_COLUMN, phon1);
            initialValues.put(PHONE2_COLUMN, phon2);
            initialValues.put(PHONE3_COLUMN, phon3);
            initialValues.put(PHONE4_COLUMN, phon4);
            initialValues.put(UNAME_COLUMN, name);
            initialValues.put(UADDR_COLUMN, address);

            return mDb.insert(DATABASE_TABLE, null, initialValues);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            Log.w("TaskDBAdapter", "Upgrading from version" + oldVersion
                    + " to " + newVersion + ", which will destroy all old data");
            // Upgrade the existing database to conform to the new
            // version. Multiple previous versions can be handled by
            // comparing oldVersion and newVersion values.
            // The simplest case is to drop the old table and create a new one.
            db.execSQL("DROP TABLE IF IT EXISTS " + DATABASE_TABLE);
            // Create a new one.
            onCreate(db);
        }

    }
}

3 个答案:

答案 0 :(得分:3)

活动不应该有构造函数!所有这些都应该在生命周期回调中完成。将内容从ctor移到onCreate()并删除ctor:

public Setupemail(Context ctx) {
    this.context = ctx;
    DBHelper = new SqlOpenHelper(context);
}

答案 1 :(得分:2)

放弃这个:

public Setupemail(Context ctx) {
    this.context = ctx;
    DBHelper = new SqlOpenHelper(context);
}

并删除context字段。 Setupemail类继承自Activity,它是Context本身的子类。如果您想引用Context,可以在内部类中使用getBaseContext,甚至可以在Setupemail类中省略它。

答案 2 :(得分:-1)

你没有一个空的构造函数。需要在定义其他构造函数时定义

public Setupmail()
{
}