2012-01-16 68 views
-2

嗨我正在与我的应用程序项目..它只是发生,我点击登录按钮后,弹出一个错误消息说,对不起!应用程序意外停止。请再试一次。这是什么错误?请帮助谢谢Android应用程序 - 导致日志记录错误

这是我对用户的数据库代码..

package com.gomez.android; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class dbuser{ 


public static final String user_name = "username"; 
public static final String user_password = "password"; 
public static final String row_id = "_id"; 

private static final String TAG = "UdbAdapter"; 
private DatabaseHelper UdbHelper; 
private SQLiteDatabase Udb; 

/** 
* Database creation sql statement 
*/ 
private static final String DATABASE_CREATE = 
    "create table pages (_id integer primary key autoincrement, " 
    + "username text not null, password text not null);"; 

private static final String DATABASE_NAME = "UserAccount"; 
private static final String DATABASE_TABLE = "User"; 
private static final int DATABASE_VERSION = 1; 

private Context context = null; 

private static class DatabaseHelper extends SQLiteOpenHelper { 

    DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

     db.execSQL(DATABASE_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
       + newVersion + ", which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXISTS pages"); 
     onCreate(db); 
    } 
} 

public dbuser(Context cntxt) { 
    this.context = cntxt; 
    UdbHelper = new DatabaseHelper(context); 
} 

public void open() throws SQLException { 
    Udb = UdbHelper.getWritableDatabase(); 
} 

public void close() { 
    UdbHelper.close(); 
} 

public long createaccount(String username, String password) 
{ 
    Udb = UdbHelper.getWritableDatabase(); 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(user_name, username); 
    initialValues.put(user_password, password); 

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

public boolean Login(String username, String password)throws SQLException 
{ 
    Cursor mCursor = Udb.rawQuery(DATABASE_TABLE, new String[]{username,password}); 
    if (mCursor != null){ 
     if(mCursor.getCount() > 0){ 
      return true; 
     } 
    } 
    return false; 
} 

}

,这是我的登录页面的代码.....

package com.gomez.android; 

import com.gomez.android.dbuser; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 



public class login extends Activity{ 
//Declare views 
private EditText uname; 
private EditText pword; 
private Button btnlogin; 
private Button btncancel; 



/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //set Activity Layout 
    setContentView(R.layout.login); 

    //Get EditText and Button References 
    uname = (EditText)findViewById(R.id.username); 
    pword = (EditText)findViewById(R.id.password); 
    btnlogin = (Button)findViewById(R.id.login_enter); 
    btncancel = (Button)findViewById(R.id.cancel); 

    //set Click Listener 
    btnlogin.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      //Check Login 
      final String username = uname.getText().toString(); 
      final String password = pword.getText().toString(); 


      dbuser users = new dbuser(login.this); 
      users.open(); 
      if(users.Login(username, password)){ 
        if(username.equalsIgnoreCase(username)&& password.equalsIgnoreCase(password)) 
        { 
         Toast.makeText(login.this,"Successfully Logged In", Toast.LENGTH_LONG).show(); 
         Intent i = new Intent(login.this, firstpage.class); 
         startActivity(i); 
        } 
        else{ 
         Toast.makeText(login.this,"Invalid Username/Password", Toast.LENGTH_LONG).show(); 
        } 
        users.close(); 
       } 
      } 


    }); 

    btncancel.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      //close application 
      finish(); 
     } 
    }); 
} 

}

+0

不要低估LogCat的威力! – Selvin 2012-01-16 17:28:47

+2

使用你的logcat,你也需要把相关的代码,没有人会通过所有的代码没有方向从logcat – 2012-01-16 17:51:52

+0

通读请寄出你的堆栈跟踪输出。 – bschultz 2012-01-16 18:21:34

回答

0

我不能确定你的代码有什么问题,因为你没有提供任何信息LogCat,但有可能getWriteableDatabase()在运行应用程序时返回缓存的数据库(我经常在第一次执行和测试数据库时遇到此问题)。

为了确保这不是问题,在从Eclipse运行应用程序之前,请转到设置 - >应用程序 - >管理应用程序 - > [您的应用程序] - >清除数据。这是确保您在实施/测试数据库的早期阶段使用最新实现的简单方法。

这很好,可能不是问题,但它是其中一件事情,你不会指望成为问题(这往往会导致几个小时的挫折),所以我想我会提出它: )。

+0

Alex Lockwood先生,这里是LogCat信息: 01-16 13:18:39.762:D/SntpClient(68):请求时间失败:java.net.SocketException:地址族不受协议支持 谢谢 – jonael08 2012-01-17 14:20:26

+0

谢谢您。这有助于 – jonael08 2012-06-03 04:40:12