简体   繁体   English

Android中的登录应用程序

[英]Login Application in Android

I am creating a Login Application in Android, I've done pushing the database into the emulator. 我正在Android中创建一个Login Application,已将数据库推入模拟器。 When I add database using the application I made, the emulator stops working and stop responding. 当我使用自己制作的应用程序添加数据库时,模拟器停止工作并停止响应。 What should I do? 我该怎么办?

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); 
           */ 
            }
  }

The application stops responding in this point: 应用程序此时停止响应:

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


                   } 
           }); 

My Guess the problem raising here 我猜这里出现的问题

Welcome createaccount = new Welcome(); 

you have extends the Application class to get the Application Context. 您已经扩展了Application类以获取Application Context。 But you may not register in the AndroidManifest.xml file. 但是您可能无法在AndroidManifest.xml文件中注册。 So Please set the android:name="Welcome" for <application> tag in the Manifest file. 因此,请在清单文件中为<application>标记设置android:name="Welcome"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM