当我尝试在HomeActivity类中初始化单例类时,我收到此异常。
在HomeActivity中,我这样称呼:dbo = DatabaseOperations.getInstance();
DatabaseOperations是我的单例类。我被卡住了请帮助我。
生成异常:
03-27 15:05:12.825: E/AndroidRuntime(27468): FATAL EXCEPTION: main
03-27 15:05:12.825: E/AndroidRuntime(27468): java.lang.ExceptionInInitializerError
03-27 15:05:12.825: E/AndroidRuntime(27468): at de.novalyst.authenticvpn.gui.HomeActivity$1.onClick(HomeActivity.java:65)
03-27 15:05:12.825: E/AndroidRuntime(27468): at android.view.View.performClick(View.java:4240)
03-27 15:05:12.825: E/AndroidRuntime(27468): at android.view.View$PerformClick.run(View.java:17721)
03-27 15:05:12.825: E/AndroidRuntime(27468): at android.os.Handler.handleCallback(Handler.java:730)
03-27 15:05:12.825: E/AndroidRuntime(27468): at android.os.Handler.dispatchMessage(Handler.java:92)
03-27 15:05:12.825: E/AndroidRuntime(27468): at android.os.Looper.loop(Looper.java:137)
03-27 15:05:12.825: E/AndroidRuntime(27468): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-27 15:05:12.825: E/AndroidRuntime(27468): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 15:05:12.825: E/AndroidRuntime(27468): at java.lang.reflect.Method.invoke(Method.java:525)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-27 15:05:12.825: E/AndroidRuntime(27468): at dalvik.system.NativeStart.main(Native Method)
03-27 15:05:12.825: E/AndroidRuntime(27468): Caused by: com.db4o.ext.Db4oIOException: /storage/emulated/0/authenticvpn.db4o: open failed: EISDIR (Is a directory)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.db4o.io.RandomAccessFileFactory.newRandomAccessFile(RandomAccessFileFactory.java:26)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.db4o.io.FileStorage$FileBin.<init>(FileStorage.java:43)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.db4o.io.FileStorage.open(FileStorage.java:22)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.db4o.io.StorageDecorator.open(StorageDecorator.java:27)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.db4o.io.CachingStorage.open(CachingStorage.java:52)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.db4o.internal.IoAdaptedObjectContainer.openImpl(IoAdaptedObjectContainer.java:57)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.db4o.internal.ObjectContainerBase$1.run(ObjectContainerBase.java:140)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.db4o.foundation.DynamicVariable.with(DynamicVariable.java:54)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.db4o.foundation.Environments.runWith(Environments.java:28)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.db4o.internal.ObjectContainerBase.withEnvironment(ObjectContainerBase.java:161)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.db4o.internal.ObjectContainerBase.open(ObjectContainerBase.java:131)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.db4o.internal.IoAdaptedObjectContainer.<init>(IoAdaptedObjectContainer.java:35)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.db4o.internal.ObjectContainerFactory.openObjectContainer(ObjectContainerFactory.java:18)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.db4o.Db4oEmbedded.openFile(Db4oEmbedded.java:65)
03-27 15:05:12.825: E/AndroidRuntime(27468): at de.novalyst.authenticvpn.database.DatabaseOperations.openDB(DatabaseOperations.java:61)
03-27 15:05:12.825: E/AndroidRuntime(27468): at de.novalyst.authenticvpn.database.DatabaseOperations.<init>(DatabaseOperations.java:36)
03-27 15:05:12.825: E/AndroidRuntime(27468): at de.novalyst.authenticvpn.database.DatabaseOperations.<clinit>(DatabaseOperations.java:18)
03-27 15:05:12.825: E/AndroidRuntime(27468): ... 12 more
03-27 15:05:12.825: E/AndroidRuntime(27468): Caused by: java.io.FileNotFoundException: /storage/emulated/0/authenticvpn.db4o: open failed: EISDIR (Is a directory)
03-27 15:05:12.825: E/AndroidRuntime(27468): at libcore.io.IoBridge.open(IoBridge.java:409)
03-27 15:05:12.825: E/AndroidRuntime(27468): at java.io.RandomAccessFile.<init>(RandomAccessFile.java:118)
03-27 15:05:12.825: E/AndroidRuntime(27468): at java.io.RandomAccessFile.<init>(RandomAccessFile.java:150)
03-27 15:05:12.825: E/AndroidRuntime(27468): at com.db4o.io.RandomAccessFileFactory.newRandomAccessFile(RandomAccessFileFactory.java:19)
03-27 15:05:12.825: E/AndroidRuntime(27468): ... 28 more
03-27 15:05:12.825: E/AndroidRuntime(27468): Caused by: libcore.io.ErrnoException: open failed: EISDIR (Is a directory)
03-27 15:05:12.825: E/AndroidRuntime(27468): at libcore.io.Posix.open(Native Method)
03-27 15:05:12.825: E/AndroidRuntime(27468): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
03-27 15:05:12.825: E/AndroidRuntime(27468): at libcore.io.IoBridge.open(IoBridge.java:393)
03-27 15:05:12.825: E/AndroidRuntime(27468): ... 31 more
03-27 15:10:12.958: I/Process(27468): Sending signal. PID: 27468 SIG: 9
我的单身人士课程是:
import java.io.File;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.query.Query;
import de.novalyst.authenticvpn.gui.Constants;
import android.util.Log;
public class DatabaseOperations {
private ObjectContainer db;
private static DatabaseOperations db4oDbInstance = new DatabaseOperations();
EmbeddedConfiguration config;
private Query query;
private Users user;
public static DatabaseOperations getInstance( ) {
if (db4oDbInstance == null) {
db4oDbInstance = new DatabaseOperations();
}
return db4oDbInstance;
}
public DatabaseOperations() {
Log.i(null, Constants.DB_CONNECT);
config = Db4oEmbedded.newConfiguration();
config.common().objectClass(Users.class).cascadeOnDelete(true);
db = openDB();
if(db.equals(null)){
Log.e(null, Constants.DB_ERROR);
} else
Log.i(null, Constants.DB_SUCCESS);
//db.ext().configure().objectClass(Users.class).cascadeOnUpdate(true);
//db.ext().configure().objectClass(Users.class).updateDepth(5);
}
private ObjectContainer openDB() {
File fulPath = new File(Constants.DATABASE_NAME);
if (!fulPath.exists()) {
System.out.println("No DB4O Directory or file Exists! Creating...");
boolean cretRet = fulPath.mkdirs();
if(cretRet) {
System.out.println("Created Directory Successfully");
} else {
System.out.println("ERROR Creating Directory!!!");
return null;
}
}
return Db4oEmbedded.openFile(config, Constants.DATABASE_NAME);
}
public ObjectContainer getDb() {
return db;
}
public void setDb(ObjectContainer db) {
this.db = db;
}
public boolean closeConn() {
boolean statusConn = false;
if(db != null){
Log.i(null, Constants.DB_CLOSE);
getDb().close();
statusConn = true;
}
return statusConn;
}
public void storeUser(Users user){
getDb().store(user);
}
public boolean checkUser(String mobile){
query = getDb().query();
query.constrain(Users.class);
query.descend("mobile").constrain(mobile);
ObjectSet<Users> result = query.execute();
System.out.println("checkuser"+result);
if (result.hasNext())
return true;
else
return false;
}
public Users retrieveUser(String mobile){
query = getDb().query();
query.constrain(Users.class);
query.descend("mobile").constrain(mobile);
ObjectSet<Users> result = query.execute();
user = (Users) result.next();
return user;
}
}
HomeActivity从我调用单例类的地方开始。
import java.io.File;
import com.example.authenticvpn.R;
import de.novalyst.authenticvpn.database.CreateUserDb4o;
import de.novalyst.authenticvpn.database.DatabaseOperations;
import de.novalyst.authenticvpn.database.Users;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
public class HomeActivity extends Activity {
private String mobileNumber;
private Users user;
private CreateUserDb4o createuser;
DatabaseOperations dbo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//dbo = DatabaseOperations.getInstance();
//new File(Constants.DATABASE_NAME).delete();
mobileNumber = readMobileNumber(); // reading user mobile number
String key = "123456789aaaaaaa"; // 16 digit random number
byte[] keyValue = key.getBytes();
String data = "Novalyst_30514";
System.out.println("Original PIN:"+data);
try {
String encryptvalue = CryptoAlgorithm.encrypt(data, keyValue);
String decryptvalue = CryptoAlgorithm.decrypt(encryptvalue, keyValue);
String hmacencodedvalue = CryptoAlgorithm.encodeHMAC("chaubey",encryptvalue);
System.out.println("Encrypted pin is:"+encryptvalue);
System.out.println("Decrypted pin is:"+decryptvalue);
System.out.println("HMAC encrypted value is:"+hmacencodedvalue);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("HomeActivity Exception");
e.printStackTrace();
}
findViewById(R.id.enrollButton).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// No account, load new account view
**dbo = DatabaseOperations.getInstance();**
boolean flag = dbo.checkUser(mobileNumber);
if(flag)
{
user = dbo.retrieveUser(mobileNumber);
if(user.getFlag() == true){
System.out.println("connection status home1:");
Intent intent = new Intent(HomeActivity.this,LoginActivity.class);
startActivityForResult(intent, 0);
}
else {
System.out.println("connection status home2: ");
Intent intent = new Intent(HomeActivity.this,EnrollmentActivity.class);
startActivityForResult(intent, 0);
}
}
else
{
createuser = new CreateUserDb4o();
createuser.execute(mobileNumber);
System.out.println("Creating user....");
Intent intent = new Intent(HomeActivity.this,EnrollmentActivity.class);
startActivityForResult(intent, 0);
}
}
});
}
public String readMobileNumber(){
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
return (String)telephonyManager.getLine1Number();
}
@Override
protected void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("######## onDestroy called");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
}
我已将openDB()方法更改为:
private ObjectContainer openDB() {
if ((Constants.DATABASE_NAME) !=null) {
System.out.println("Created db Successfully");
return Db4oEmbedded.openFile(config, Constants.DATABASE_NAME);
} else {
System.out.println("ERROR Creating db!!!");
return null;
}
}
仍然没有运气