Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Everytime I try to run my application it shows up a dialog saying application has stopped unexpectedly, both on phone and emulator. The program has no errors and i've tried cleaning my project and several solutions but non of them worked. the same problem occurs with several projects even the hello world project.

This is my main activity: MainActivity.java

package com.noura.luba;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {


    TextView forgotPass, createAcc;
    Button loginButton;
    EditText userID, pass;
    LoginDataBaseAdapter loginDataBaseAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);

             loginDataBaseAdapter=new LoginDataBaseAdapter(this);
             loginDataBaseAdapter=loginDataBaseAdapter.open();

             forgotPass= (TextView)findViewById(R.id.forgotPasswordTextView);
             createAcc= (TextView)findViewById(R.id.createAccountTextView);
             loginButton= (Button)findViewById(R.id.loginButton);
             userID=(EditText)findViewById(R.id.userIDEditText);
             pass=(EditText)findViewById(R.id.passwordEditText);

             createAcc.setOnClickListener(new View.OnClickListener() {

                 public void onClick(View v) {

                     Intent intentCreateAcc = new Intent(getApplicationContext(), CreateAccActivity.class);
                     startActivity(intentCreateAcc);

                 }
             });

           forgotPass.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
             Intent intentForgotPass = new Intent(getApplicationContext(), ForgotPassActivity.class);
                 startActivity(intentForgotPass);
            }
             }); 

             loginButton.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) {

                     String id=userID.getText().toString();

                     Intent intentLogin = new Intent(getApplicationContext(), Home.class);

                     intentLogin.putExtra("User_ID", id);
                     startActivity(intentLogin);

                 }

             });


    }

    public void LogIn(View V)
    {

        final Dialog dialog =  new Dialog(MainActivity.this);

        dialog.setContentView(R.layout.activity_main);
        dialog.setTitle("LUBA LogIn");

        final EditText userID=(EditText)dialog.findViewById(R.id.userIDEditText);
        final EditText pass=(EditText)dialog.findViewById(R.id.passwordEditText);

        Button login = (Button)dialog.findViewById(R.id.loginButton);


        //Set On ClickListener
        login.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                String userName= userID.getText().toString();
                String password=pass.getText().toString();

                String storedPassword = loginDataBaseAdapter.getSingleEntry(userName);

                if(password.equals(storedPassword))
                {
                    Toast.makeText(MainActivity.this, "Login Successful", Toast.LENGTH_LONG).show();
                    dialog.dismiss();

                }
                else
                {

                    Toast.makeText(MainActivity.this, "User Name and Password do not match", Toast.LENGTH_LONG).show();

                }
            }
        });

        dialog.show();
    }



    protected void onDestroy() {

        super.onDestroy();

        loginDataBaseAdapter.close();

    }


}

This is my xml page: activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    tools:context="com.noura.luba.MainActivity"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/blue" >

    <ImageView
        android:id="@+id/LUBAimageView"
        android:layout_width="fill_parent"
        android:layout_height="160dp"
        android:contentDescription="@string/app_name"
        android:src="@drawable/ic_launcher" />

    <EditText
        android:id="@+id/userIDEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/LUBAimageView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:ems="10"
        android:inputType="number"
        android:text="@string/user_edit_text"
        android:textColor="@color/yellow"
         >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/passwordEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/userIDEditText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:ems="10"
        android:inputType="textPassword"
        android:text="@string/password_edit_text"
        android:textColor="@color/yellow" />

    <Button
        android:id="@+id/loginButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_below="@+id/passwordEditText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:text="@string/login_button"
        android:textColor="@color/yellow"
        android:onClick="LogIn"  />



     <TextView
         android:id="@+id/forgotPasswordTextView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/LUBAimageView"
         android:layout_alignStart="@+id/LUBAimageView"
         android:layout_below="@+id/loginButton"
         android:layout_marginTop="20dp"
         android:autoLink="web"
         android:clickable="true"
         android:ems="8"
         android:onClick="forgotPassword"
         android:text="@string/forgot_password"
         android:textColor="@color/yellow" />

     <TextView
         android:id="@+id/createAccountTextView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBaseline="@+id/forgotPasswordTextView"
         android:layout_alignBottom="@+id/forgotPasswordTextView"
         android:layout_alignRight="@+id/LUBAimageView"
         android:layout_alignEnd="@+id/LUBAimageView"
         android:onClick="createAccount"
         android:text="@string/create_account"
         android:textColor="@color/yellow" />

     <include
         android:layout_width="fill_parent"
         android:layout_height="40dp"
         android:layout_alignLeft="@+id/forgotPasswordTextView"
         android:layout_alignParentBottom="true"
         android:layout_alignParentEnd="true"
         android:layout_alignParentRight="true"
         android:layout_alignStart="@+id/forgotPasswordTextView"
         layout="@layout/noura"
         android:gravity="bottom" />

</RelativeLayout>

this is my loginDatabaseAdapter.java

package com.noura.luba;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

public class LoginDataBaseAdapter {

    static final String DATABASE_NAME = "seniorLUBA";
       static final int DATABASE_VERSION = 1;

       public static final int NAME_COLUMN = 1;

       public SQLiteDatabase db;
       private final Context context;

       private DataBaseHelper dbHelper;

       public LoginDataBaseAdapter(Context _context)
       {

           context = _context;
           dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);

       }

       public LoginDataBaseAdapter open() throws SQLException
       {
           db=dbHelper.getWritableDatabase();
           return this;
       }

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

       public SQLiteDatabase getDatabaseInstance()
       {
           return db;
       }

       public void insertEntry(String userName, String password)
       {

           ContentValues newValues = new ContentValues();
           newValues.put("USERNAME", userName);
           newValues.put("PASSWORD", password);

           db.insert("student", null, newValues);
           Toast.makeText(context,  "User Info Saved", Toast.LENGTH_LONG).show();

       }

       public int deleteEntry(String UserName)
       {

           String where="StdId=?";
           int numberOFEntriesDeleted = db.delete("student", where, new String[]{UserName});
           Toast.makeText(context, "Number of Entry Deleted Successfully : " +numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
           return numberOFEntriesDeleted;
       }

       public String getSingleEntry(String userName)
       {

           Cursor cursor=db.query("student", null, " USERNAME=?", new String[]{userName}, null, null, null);
           if(cursor.getCount()<1)
               return "DOES NOT EXIST";
           cursor.moveToFirst();
           String password= cursor.getString(cursor.getColumnIndex("StdPass"));
           return password;
       }



public void updateEntry(String userName, String password)
{
    ContentValues updatedValues = new ContentValues();

    updatedValues.put("UserName", userName);
    updatedValues.put("Password", password);

    String where="UserName = ?";
    db.update("student", updatedValues, where, new String[]{userName});
}
}

this is my dataBaseHelper,java

package com.noura.luba;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHelper extends SQLiteOpenHelper {

    public DataBaseHelper(Context context, String name, CursorFactory factory, int version)
    {
        super(context, name, factory, version);

    }

     public void onCreate(SQLiteDatabase _db) 
        {
                _db.execSQL(LoginDataBaseAdapter.DATABASE_NAME);

        }

    public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
    {

        Log.w("TaskDBAdapter","Upgrading from version" +_oldVersion + "to" +_newVersion + ", which will destroy all old data");

        _db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");

        onCreate(_db);

    }


}

and this is my logcat

11-14 02:26:34.739: I/Database(347): sqlite returned: error code = 1, msg = near "seniorLUBA": syntax error
11-14 02:26:34.749: E/Database(347): Failure 1 (near "seniorLUBA": syntax error) on 0x2bee48 when preparin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
127 views
Welcome To Ask or Share your Answers For Others

1 Answer

Having a quick look at your logcat exception I can see an SQLite error on this line:

_db.execSQL(LoginDataBaseAdapter.DATABASE_NAME);

What you are trying to do is to execute an SQL with the data base name "seniorLUBA.db" only, which is not a valid SQL. Try removing that line.

You should place valid SQL statements under the onCreate method of your dataBaseHelper class only if you need to initialize the DB or perform some other custom logic for your DB.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...