Android应用程序将无法启动 [英] Android application won't start

查看:166
本文介绍了Android应用程序将无法启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我尝试运行我的应用程序它显示一个对话框,说应用程序意外停止,无论是在手机和模拟器。该程序没有错误,我试过清洗我的项目和一些解决方案,但非他们工作。有几个项目甚至Hello World项目出现相同的问题。

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.

这是我的主要活动:MainActivity.java

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

    }


}

这是我的XML页面:activity_main.xml中

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>

这是我的loginDatabaseAdapter.java

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

这是我的dataBaseHelper,JAVA

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

    }


}

这是我的logcat

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 preparing 'seniorLUBA'.
11-14 02:26:34.809: D/AndroidRuntime(347): Shutting down VM
11-14 02:26:34.809: W/dalvikvm(347): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-14 02:26:34.869: E/AndroidRuntime(347): FATAL EXCEPTION: main
11-14 02:26:34.869: E/AndroidRuntime(347): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.noura.luba/com.noura.luba.MainActivity}: android.database.sqlite.SQLiteException: near "seniorLUBA": syntax error: seniorLUBA
11-14 02:26:34.869: E/AndroidRuntime(347):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-14 02:26:34.869: E/AndroidRuntime(347):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-14 02:26:34.869: E/AndroidRuntime(347):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-14 02:26:34.869: E/AndroidRuntime(347):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-14 02:26:34.869: E/AndroidRuntime(347):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 02:26:34.869: E/AndroidRuntime(347):  at android.os.Looper.loop(Looper.java:123)
11-14 02:26:34.869: E/AndroidRuntime(347):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-14 02:26:34.869: E/AndroidRuntime(347):  at java.lang.reflect.Method.invokeNative(Native Method)
11-14 02:26:34.869: E/AndroidRuntime(347):  at java.lang.reflect.Method.invoke(Method.java:507)
11-14 02:26:34.869: E/AndroidRuntime(347):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-14 02:26:34.869: E/AndroidRuntime(347):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-14 02:26:34.869: E/AndroidRuntime(347):  at dalvik.system.NativeStart.main(Native Method)
11-14 02:26:34.869: E/AndroidRuntime(347): Caused by: android.database.sqlite.SQLiteException: near "seniorLUBA": syntax error: seniorLUBA
11-14 02:26:34.869: E/AndroidRuntime(347):  at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
11-14 02:26:34.869: E/AndroidRuntime(347):  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
11-14 02:26:34.869: E/AndroidRuntime(347):  at com.noura.luba.DataBaseHelper.onCreate(DataBaseHelper.java:18)
11-14 02:26:34.869: E/AndroidRuntime(347):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:126)
11-14 02:26:34.869: E/AndroidRuntime(347):  at com.noura.luba.LoginDataBaseAdapter.open(LoginDataBaseAdapter.java:32)
11-14 02:26:34.869: E/AndroidRuntime(347):  at com.noura.luba.MainActivity.onCreate(MainActivity.java:28)
11-14 02:26:34.869: E/AndroidRuntime(347):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-14 02:26:34.869: E/AndroidRuntime(347):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-14 02:26:34.869: E/AndroidRuntime(347):  ... 11 more
11-14 02:26:37.888: I/Process(347): Sending signal. PID: 347 SIG: 9

谁能帮我找到这个问题的解决方案?
我想提一提,我是新来的整个Android和XML环境,我已经搜查了这么多的解决方案,诉诸张贴在这里我的问题了。

could anyone please help me find the solution for this problem? I would like to mention that i'm new to the whole android and xml environment and i've searched for so many solutions before resorting to posting my question here.

推荐答案

有一个快速浏览一下你的logcat的例外,我可以看到在这条线一个SQLite错误:

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

_db.execSQL(LoginDataBaseAdapter.DATABASE_NAME);

_db.execSQL(LoginDataBaseAdapter.DATABASE_NAME);

你所要做的是与数据库名seniorLUBA.db而已,这不是一个有效的SQL执行SQL。尝试删除该行。

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.

您应该将有效的SQL语句,你dataBaseHelper类的onCreate方法下只有当你需要初始化数据库或您的数据库执行一些其他自定义逻辑。

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.

这篇关于Android应用程序将无法启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆