2010-12-11 98 views
0

我在Android中创建一个登录应用程序,我已将数据库推入模拟器。当我使用我制作的应用程序添加数据库时,模拟器停止工作并停止响应。我该怎么办?在Android中登录应用程序

package com.android.sms.privacy; 

import java.util.Arrays; 
import java.util.Locale; 

import com.android.sms.privacy.DatabaseHelper; 

import android.app.Activity; 
import android.widget.EditText; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.widget.Button; 
import android.view.View; 
import android.content.ContentValues; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.util.Log; 
import android.widget.Toast; 



public class SMS extends Activity { 
// 
public static final String DEBUG_TAG = "FullDatabase Log"; 
    public static final int DB_version = 2; 
    //public static final String DB_name = "User.db"; 
    public static final String TABLE_NAME = "login"; 
    //public static final String TABLE_NAME = "tbl_temp"; 
    //public static final String DB_name = "User_temp"; 
    public static final String DBNAME = "login"; 
    public DatabaseHelper database_helper; 
    public SQLiteDatabase database; 
    public SQLiteDatabase database_rd; 
    /** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.createaccount); 
    Toast.makeText(this,"Before Bundled", 
    Toast.LENGTH_SHORT).show(); 
    createDataBase(); 
    //LogIn login = new LogIn(); 
    Button submit = (Button)findViewById(R.id.Button02); 
    Toast.makeText(this,"after Bundled", 
    Toast.LENGTH_SHORT).show(); 
    submit.setOnClickListener(new View.OnClickListener() 
    { 
        @Override 
        public void onClick(View v) { 
          // TODO Auto-generated method stub 

          Welcome createaccount = new Welcome(); 
          Toast.makeText(createaccount.getApplicationContext(), "In Helper executing onCreate1()", 
          Toast.LENGTH_SHORT).show(); 

          String[] success = insertRecords(); 
          if(success[0].equalsIgnoreCase("1")) 
          { 
          setContentView(R.layout.menu); 
          } 


        } 
      }); 
    //database.close(); 
    } 

// 



protected String[] insertRecords() { 
Toast.makeText(this,"In insertrecords", Toast.LENGTH_SHORT).show(); 
    //database.openDatabase(DB_name,database.create(factory), 
    //ACCESSIBILITY_SERVICE); 
    final EditText username = (EditText)findViewById(R.id.username); 
    final EditText password = (EditText)findViewById(R.id.password); 
    Long newUserID = 0l; 
    String user = username.getText().toString(); 
    String userpass = password.getText().toString(); 
    ContentValues values = new ContentValues(); 
    values.put("username",user); 
    values.put("password",userpass); 
    //StringBuilder info = new StringBuilder(); 
    String[] info = new String[10]; 
    database.beginTransaction(); 
    try 
    { 
    Toast.makeText(this,"Before calling inserting values", 
    Toast.LENGTH_SHORT).show(); 
    newUserID = database.insertOrThrow(TABLE_NAME, null, values); 
    Toast.makeText(this,"After calling inserting values", 
    Toast.LENGTH_SHORT).show(); 
    info[0] = newUserID.toString(); 
    info[1] = user; 
    info[2] = userpass; 
    System.out.println(newUserID); 
    Toast.makeText(this, newUserID.toString(), 
    Toast.LENGTH_SHORT).show(); 
    Cursor c = database_rd.query(TABLE_NAME, null, null, null, null, 
    null,null); 
    LogCursorInfo(c); 
    c.close(); 
    } 
    catch(Exception e) 
    { 
    //Transaction failed; 
    e.printStackTrace(); 
    } 
    finally 
    { 
    database.endTransaction(); 
    database.close(); 
    } 
    return info; 
    } 

private void LogCursorInfo(Cursor c) { 
// TODO Auto-generated method stub 
Log.i(DEBUG_TAG, "*** Cursor Begin *** " + " Results:" + 
    c.getCount() + " Columns: " + c.getColumnCount()); 
        // Print column names 
        String rowHeaders = "|| "; 
        for (int i = 0; i < c.getColumnCount(); i++) { 
          rowHeaders = rowHeaders.concat(c.getColumnName(i) + " || "); 
        } 
        Log.i(DEBUG_TAG, "COLUMNS " + rowHeaders); 
        //Integer rows = c.getCount(); 
        //Toast.makeText(this, rows.toString(), Toast.LENGTH_SHORT).show(); 
        // Print records 
        c.moveToFirst(); 
        while (c.isAfterLast() == false) { 
          String rowResults = "|| "; 
          for (int i = 0; i < c.getColumnCount(); i++) { 
            rowResults = rowResults.concat(c.getString(i) + " || "); 
          } 
          Log.i(DEBUG_TAG, "Row " + c.getPosition() + ": " + rowResults); 
          Toast.makeText(this, rowResults.toString(), 
    Toast.LENGTH_SHORT).show(); 
          //Toast.makeText(this, "In LogCursorInfo", 

          c.moveToNext(); 
        } 
        Log.i(DEBUG_TAG, "*** Cursor End ***"); 
      } 
     void createDataBase() 
     { 
     if(Arrays.binarySearch(databaseList(), DBNAME)>=0) 
      { 
        deleteDatabase(DBNAME); 
      } 
      database_helper = new 
      DatabaseHelper(this.getApplicationContext()); 
      //database_helper.onCreate(database); 
      database = database_helper.getWritableDatabase(); 
      database.setLocale(Locale.getDefault()); // Set the local 
      database.setLockingEnabled(true); // SQLiteDatabase is made thread-safe by using locks around critical sections. 
      database_rd = database_helper.getReadableDatabase(); 
      Toast.makeText(this, "created database", 
    Toast.LENGTH_SHORT).show(); 
      /* 
      database = 
    openOrCreateDatabase(DB_name,SQLiteDatabase.CREATE_IF_NECESSARY, 
    null); 
      database.setLocale(Locale.getDefault()); // Set the local 
      database.setLockingEnabled(true); // SQLiteDatabase is made 
    thread-safe by using locks around critical sections. 
      database.setVersion(1); 
      database.execSQL(CREATE_USER_TABLE); 
      */ 
      } 
    } 

应用程序停止在这一点上回应:

submit.setOnClickListener(new View.OnClickListener() 
    { 
        @Override 
        public void onClick(View v) { 
          // TODO Auto-generated method stub 

          Welcome createaccount = new Welcome(); 
          Toast.makeText(createaccount.getApplicationContext(), "In Helper executing onCreate1()", 
          Toast.LENGTH_SHORT).show(); 

          String[] success = insertRecords(); 
          if(success[0].equalsIgnoreCase("1")) 
          { 
           setContentView(R.layout.menu); 
          } 


        } 
      }); 
+0

kyle你知道应用程序停止响应的代码在哪一点?这是一个问题的很多代码。试图完整地阅读它并理解你的问题有点难,如果你自己可以缩小它的范围并且只粘贴一部分代码,那将是非常棒的。 – Janusz 2010-12-11 08:57:12

+0

我已经添加了应用程序停止的代码。 – sean 2010-12-11 09:09:10

+0

你可以添加你的adb logcat输出吗? – ggomeze 2010-12-11 09:25:09

回答

1

我想这个问题在这里提出

Welcome createaccount = new Welcome(); 

你扩展了应用类来获取应用程序上下文。但是您不可以在AndroidManifest.xml文件中注册。所以请在清单文件中为<application>标签设置android:name="Welcome"标签。