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

Posted

技术标签:

【中文标题】我的 Android 应用程序在模拟器上运行,但无法在我的 android 设备上运行【英文标题】:My Android application runs on emulator but not working on my android device 【发布时间】:2017-07-03 01:27:47 【问题描述】:

我的应用程序在 android 模拟器上运行良好。当我试图在我的设备上运行它时,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_
    android:layout_ 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_
        android:layout_
        android:layout_weight="1"
        android:gravity="center_vertical|center_horizontal">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_
            android:layout_
            android:src="@drawable/logo" />



    </LinearLayout>

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

                <LinearLayout
                    android:id="@+id/email_login_form"
                    android:layout_
                    android:layout_
                    android:orientation="vertical" >

                    <AutoCompleteTextView android:id="@+id/email" android:layout_
                        android:layout_ 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_
                        android:layout_ 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_
                        android:layout_
                        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_
        android:layout_
        android:layout_weight="1"
        android:gravity="bottom|center_horizontal">

        <TextView
            android:layout_
            android:layout_
            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();
    


有人可以帮忙吗?

【问题讨论】:

DATABASE_CREATE 字符串在哪里使用?你能把它删掉吗? 我在另一个名为 DataBaseHelper 的类中使用 DATABASE_CREATE String。正如我在模拟器上所说,一切正常。 显示那个助手类。 【参考方案1】:

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

static final int DATABASE_VERSION=2;

应该修复它。

【讨论】:

数据库架构没有变化 @Adrian-Mazurkiewicz 您可能在将应用程序安装到手机后但在将其安装到模拟器之前的某个时间更改了该列的名称【参考方案2】:

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

【讨论】:

其实有帮助 ;) 谢谢

以上是关于我的 Android 应用程序在模拟器上运行,但无法在我的 android 设备上运行的主要内容,如果未能解决你的问题,请参考以下文章

React Native 在多个同时运行的 Android 模拟器上运行

为啥我的模拟器没有在 Android Studio 上运行?

在 Android 模拟器上运行谷歌地图应用程序

android studio 不在 iso 模拟器上运行我的应用程序

在 android studio 模拟器上运行 react native 应用程序

Android Studio 应用程序可在模拟器中运行,但不能在真实设备上运行