简体   繁体   中英

My Android application runs on emulator but not working on my android device

My application works fine on android emulator. When I'm trying to run it on my device LogCat column gives me info that there is no such columns: USERNAME. On emulator my SQLite database is created normally and I can add new users and sign but in device there is an exception when I'm trying doing this.

Here is a Log file when i'm trying to create new user:

02-14 17:29:07.997: D/BubblePopupHelper(9620): isShowingBubblePopup : false
02-14 17:30:07.617: E/SQLiteLog(10274): (1) no such column: USERNAME
02-14 17:30:07.627: D/AndroidRuntime(10274): Shutting down VM
02-14 17:30:07.627: E/AndroidRuntime(10274): FATAL EXCEPTION: main
02-14 17:30:07.627: E/AndroidRuntime(10274): Process: com.example.naukamagisterka, PID: 10274
02-14 17:30:07.627: E/AndroidRuntime(10274): android.database.sqlite.SQLiteException: no such column: USERNAME (code 1): , while compiling: SELECT * FROM USERS WHERE  USERNAME=?
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:897)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:508)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:726)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1426)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1273)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1144)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1312)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at com.example.naukamagisterka.LoginDataBaseAdapter.getSingleEntryUsers(LoginDataBaseAdapter.java:81)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at com.example.naukamagisterka.SignUPActivity$1.onClick(SignUPActivity.java:44)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.view.View.performClick(View.java:4764)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.view.View$PerformClick.run(View.java:19844)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.os.Handler.handleCallback(Handler.java:739)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.os.Looper.loop(Looper.java:135)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at android.app.ActivityThread.main(ActivityThread.java:5376)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at java.lang.reflect.Method.invoke(Native Method)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at java.lang.reflect.Method.invoke(Method.java:372)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
02-14 17:30:07.627: E/AndroidRuntime(10274):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

AndroidManifest.xml:

  <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.naukamagisterka"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="17"
            android:targetSdkVersion="17" />

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".ActivityLogin"
                android:label="@string/title_activity_activity_login" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".SignUPActivity"
                android:label="@string/title_activity_sign_up" >
            </activity>
            <activity
                android:name=".HomeAfterLogin"
                android:label="@string/title_activity_home_after_login" >
            </activity>
        </application>

    </manifest>

ActivityLogin.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal"
    android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" tools:context=".LoginActivity"
    android:background="#0099CC"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true">

   <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/logo" />



    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal">
        <ProgressBar android:id="@+id/login_progress" style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:visibility="gone" />
        <ScrollView android:id="@+id/login_form" android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/email_login_form"
                    android:layout_width="match_parent"
                    android:layout_height="254dp"
                    android:orientation="vertical" >

                    <AutoCompleteTextView android:id="@+id/email" android:layout_width="match_parent"
                        android:layout_height="wrap_content" android:hint="@string/prompt_email"
                        android:inputType="textEmailAddress" android:maxLines="1"
                        android:singleLine="true"
                        android:layout_marginBottom="10dp"
                        android:textColorHint="#ffffffff"
                        android:textColor="#ffffffff" />

                    <EditText android:id="@+id/password" android:layout_width="match_parent"
                        android:layout_height="wrap_content" android:hint="@string/prompt_password"
                        android:imeActionLabel="@string/action_sign_in"
                        android:imeOptions="actionUnspecified" android:inputType="textPassword"
                        android:maxLines="1" android:singleLine="true"
                        android:layout_marginBottom="10dp"
                        android:textColorHint="#ffffffff"
                        android:textColor="#ffffffff" />

                    <Button
                        android:id="@+id/email_sign_in_button"
                        style="?android:textAppearanceSmall"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="16dp"
                        android:background="#66CCFF"
                        android:text="@string/action_sign_in"
                        android:textColor="#ffffffff"
                        android:textStyle="bold" />

                </LinearLayout>
            </LinearLayout>
        </ScrollView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="bottom|center_horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sign_up"
            android:id="@+id/signUpTextView"
            android:autoLink="web"
            android:textColor="#ffffffff" />
    </LinearLayout>
</LinearLayout>

ActivityLogin.java

package com.example.naukamagisterka;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.method.DialerKeyListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityLogin extends Activity {
    Button btnLogin;
    TextView btnSingIn;
    LoginDataBaseAdapter loginDataBaseAdapter;


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

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

        btnLogin = (Button)findViewById(R.id.email_sign_in_button);
        btnSingIn = (TextView)findViewById(R.id.signUpTextView);

        final EditText editTextEmail = (EditText)findViewById(R.id.email);
        final EditText editTextPassword = (EditText)findViewById(R.id.password);




        btnSingIn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intentSignIn = new Intent(getApplicationContext(), SignUPActivity.class);
                startActivity(intentSignIn);
            }
        });

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //try {
                    final String userName = editTextEmail.getText().toString();
                     final String userPassword = editTextPassword.getText().toString();
                      String storedPassword = loginDataBaseAdapter.getSingleEntry(userName);

                        if (storedPassword.equals(userPassword)){
                            Toast.makeText(ActivityLogin.this, "Udana próba zalogowania.", Toast.LENGTH_LONG).show();
                            setContentView(R.layout.activity_home_after_login);
                            }   
                        else{
                            Toast.makeText(ActivityLogin.this, "Niepoprawny e-mail lub hasło! Spróbuj ponownie.", Toast.LENGTH_LONG).show();
                            }
                } //catch (Exception e) {
                //
                //}

        });


    }



    @Override
    protected void onDestroy() {
        super.onDestroy();

        loginDataBaseAdapter.close();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

LoginDataBaseAdapter.java

package com.example.naukamagisterka;

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 = "WylaczSwiatloDB";
    static final int DATABASE_VERSION=1;
    public static final int NAME_COULMN=1;

    public String Email;
    public String Password;
    public int Points;

    LoginDataBaseAdapter loginDataBaseAdapter;

    static final String DATABASE_CREATE = "CREATE TABLE "+"USERS"+"(USERNAME VARCHAR, PASSWORD VARCHAR, POINTS INT)"+";"+"";

    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);
        newValues.put("POINTS", 0);

        db.insert("USERS", null, newValues);
    }

    public int deleteEntry(String UserName){
        String where = "USERNAME=?";
        int numberOFEntriesDeleted = db.delete("USERS", where, new String[]{UserName});
        return numberOFEntriesDeleted;
    }

    public String getSingleEntry(String userName){
        Cursor cursor = db.query("USERS", null, "USERNAME=?", new String[]{userName},null,null,null);
        if (cursor.getCount()<1) //Nie ma username
        {
            cursor.close();
            return "NOT EXIST";
        }

        else{
        cursor.moveToFirst();
        String password = cursor.getString(cursor.getColumnIndex("PASSWORD"));
        cursor.close();
        return password;
        }

    }

    public String getSingleEntryUsers(String userName){
        Cursor cursor = db.query("USERS", null, " USERNAME=?", new String[]{userName},null,null,null);
        if (cursor.getCount()<1) //Nie ma username
        {
            cursor.close();
            return "NOT EXIST";
        }

        else{
        cursor.moveToFirst();
        String user = cursor.getString(cursor.getColumnIndex("USERNAME"));
        cursor.close();
        return user;
        }

    }

    public void updateEntry(String userName, String password){
            ContentValues updateValues = new ContentValues();
            updateValues.put("USERNAME", userName);
            updateValues.put("PASSWORD", password);
            updateValues.put("POINTS", 0);

            String where = "USERNAME=?";
            db.update("USERS", updateValues, where, new String[]{userName});

        //String storedUsers = loginDataBaseAdapter.getSingleEntry(userName);

    }

    public void displayToast(String value){

         Toast.makeText(context, value, Toast.LENGTH_LONG).show();
    }

}

Anyone could help?

You may have changed the DB schema without increasing DATABASE_VERSION and thus the new app is finding the old DB.

static final int DATABASE_VERSION=2;

should fix it.

尝试在设备上卸载应用程序,然后重新运行它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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