每当我尝试运行我的应用程序时,它会显示一个对话框,说明应用程序已经意外停止,无论是在手机还是模拟器上。该程序没有错误,我已经尝试清理我的项目和几个解决方案,但没有他们工作。即使是hello world项目,几个项目也会出现同样的问题。
这是我的主要活动: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
<?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
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
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
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环境的新手,我已经搜索了这么多解决方案,然后才在这里发布我的问题。
答案 0 :(得分:0)
快速浏览一下logcat异常,我可以在这一行看到一个SQLite错误:
_db.execSQL(LoginDataBaseAdapter.DATABASE_NAME);
您要做的是使用数据库名称&#34; seniorLUBA.db&#34;执行SQL。只是,这不是一个有效的SQL。尝试删除该行。
只有在需要初始化数据库或为数据库执行其他自定义逻辑时,才应在dataBaseHelper类的onCreate方法下放置有效的SQL语句。