【问题标题】:Android application - Logging causing errorAndroid 应用程序 - 记录导致错误
【发布时间】:2012-02-11 14:50:59
【问题描述】:

您好,我正在处理我的应用程序项目。只是碰巧在我单击登录按钮后,会弹出一条错误消息,说对不起!应用程序意外停止。请再试一次。这是什么错误?请帮忙谢谢

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

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();
        }
    });
}

}

【问题讨论】:

  • 不要低估 LogCat 的力量!
  • 使用你的logcat,你还需要放相关代码,没有人会在没有logcat指示的情况下阅读所有代码
  • 请发布您的堆栈跟踪输出。

标签: android string sqlite passwords


【解决方案1】:

我无法确定您的代码有什么问题,因为您没有提供来自 LogCat 的任何信息,但getWriteableDatabase() 在运行应用程序时可能会返回一个缓存的数据库(我经常遇到第一次实现和测试数据库时出现这个问题)。

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

这很可能不是问题,但它是您不希望成为问题的事情之一(这通常会导致数小时的挫败感),所以我想我会提出来 :) .

【讨论】:

  • 先生。 Alex Lockwood,这里是 LogCat 信息:01-16 13:18:39.762: D/SntpClient(68): request time failed: java.net.SocketException: Address family not supported by protocol 谢谢
猜你喜欢
  • 1970-01-01
  • 2021-06-20
  • 2020-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
相关资源
最近更新 更多