java - 我的 Android 应用程序在模拟器上运行但无法在我的 Android 设备上运行

标签 java android eclipse sqlite emulation

我的应用程序在安卓模拟器上运行良好。当我试图在我的设备上运行它时,LogCat 列给我的信息是没有这样的列:USERNAME。 在模拟器上,我的 SQLite 数据库正常创建,我可以添加新用户并登录,但在我尝试执行此操作时,在设备中出现异常。

这是我尝试创建新用户时的日志文件:

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>

Activity 登录.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);
    }

}

登录数据库适配器.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();
    }

}

有人可以帮忙吗?

最佳答案

您可能在没有增加 DATABASE_VERSION 的情况下更改了数据库架构,因此新应用正在寻找旧数据库。

static final int DATABASE_VERSION=2;

应该修复它。

关于java - 我的 Android 应用程序在模拟器上运行但无法在我的 Android 设备上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42231541/

相关文章:

java - 为什么我可以在 Java 中抛出 null?

java - 如何使变量等待用户按下四个按钮之一?

android - 在移动设备上停止旋转

java - 同时开发免费版和付费版

eclipse - 如何以编程方式从完整类名中查找 Eclipse 插件中的 .java 文件?

java - 当我选择第一行时,为什么我的 JTable 重绘会发生堆栈溢出?

java - 如何在线程池队列上观察我的快速源

java - 改造 -- 预期为 BEGIN_ARRAY,但实际为 STRING

java - 任务 ':app:compileDevDebugJavaWithJavac' 执行失败

eclipse EGIT : Current branch not configred for pull