收到类似“您的应用程序意外停止,请再次尝试强制关闭"之类的错误消息. [英] Get an error like "your appliction has stoped unexpectdly .try again force close"

查看:56
本文介绍了收到类似“您的应用程序意外停止,请再次尝试强制关闭"之类的错误消息.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了android数据库,但在控制台上出现错误,例如"您的应用程序意外停止.请再次尝试强制关闭"
这是我的代码:

I have done android database but error on console like "your appliction has stoped unexpectdly .try again force close"
This is my code:

package mina.android.DatabaseDemo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
	static final String dbName="demoDB";
	static final String employeeTable="Employees";
	static final String colID="EmployeeID";
	static final String colName="EmployeeName";
	static final String colAge="Age";
	static final String colDept="Dept";
	
	static final String deptTable="Dept";
	static final String colDeptID="DeptID";
	static final String colDeptName="DeptName";
	
	static final String viewEmps="ViewEmps";
	public DatabaseHelper(Context context) {
		super(context, dbName, null,33);
		
		// TODO Auto-generated constructor stub
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
		db.execSQL("CREATE TABLE "+deptTable+" ("+colDeptID+ " INTEGER PRIMARY KEY , "+
				colDeptName+ " TEXT)");
		
		db.execSQL("CREATE TABLE "+employeeTable+" ("+colID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
				colName+" TEXT, "+colAge+" Integer, "+colDept+" INTEGER NOT NULL ,FOREIGN KEY ("+colDept+") REFERENCES "+deptTable+" ("+colDeptID+"));");
		
		
		db.execSQL("CREATE TRIGGER fk_empdept_deptid " +
				" BEFORE INSERT "+
				" ON "+employeeTable+
				
				" FOR EACH ROW BEGIN"+
				" SELECT CASE WHEN ((SELECT "+colDeptID+" FROM "+deptTable+" WHERE "+colDeptID+"=new."+colDept+" ) IS NULL)"+
				" THEN RAISE (ABORT,'Foreign Key Violation') END;"+
				"  END;");
		
		db.execSQL("CREATE VIEW "+viewEmps+
				" AS SELECT "+employeeTable+"."+colID+" AS _id,"+
				" "+employeeTable+"."+colName+","+
				" "+employeeTable+"."+colAge+","+
				" "+deptTable+"."+colDeptName+""+
				" FROM "+employeeTable+" JOIN "+deptTable+
				" ON "+employeeTable+"."+colDept+" ="+deptTable+"."+colDeptID
				);
		//Inserts pre-defined departments
		InsertDepts(db);
	}
		
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
		db.execSQL("DROP TABLE IF EXISTS "+employeeTable);
		db.execSQL("DROP TABLE IF EXISTS "+deptTable);
		
		db.execSQL("DROP TRIGGER IF EXISTS dept_id_trigger");
		db.execSQL("DROP TRIGGER IF EXISTS dept_id_trigger22");
		db.execSQL("DROP TRIGGER IF EXISTS fk_empdept_deptid");
		db.execSQL("DROP VIEW IF EXISTS "+viewEmps);
		onCreate(db);
	}
	
	 void AddEmployee(Employee emp)
	{
		 
		 
		 SQLiteDatabase db= this.getWritableDatabase();
		 
		
		ContentValues cv=new ContentValues();
		
		cv.put(colName, emp.getName());
		cv.put(colAge, emp.getAge());
		cv.put(colDept, emp.getDept());
		//cv.put(colDept,2);
		
		db.insert(employeeTable, colName, cv);
		db.close();
		
		
	}
	 
	 int getEmployeeCount()
	 {
		SQLiteDatabase db=this.getWritableDatabase();
		Cursor cur= db.rawQuery("Select * from "+employeeTable, null);
		int x= cur.getCount();
		cur.close();
		return x;
	 }
	 
	 Cursor getAllEmployees()
	 {
		 SQLiteDatabase db=this.getWritableDatabase();
		 
		 
		 
		 //Cursor cur= db.rawQuery("Select "+colID+" as _id , "+colName+", "+colAge+" from "+employeeTable, new String [] {});
		 Cursor cur= db.rawQuery("SELECT * FROM "+viewEmps,null);
		 return cur;
		 
	 }
	 
	 Cursor getAllDepts()
	 {
		 SQLiteDatabase db=this.getReadableDatabase();
		 Cursor cur=db.rawQuery("SELECT "+colDeptID+" as _id, "+colDeptName+" from "+deptTable,new String [] {});
		 
		 return cur;
	 }
	 
	 void InsertDepts(SQLiteDatabase db)
	 {
		 ContentValues cv=new ContentValues();
			cv.put(colDeptID, 1);
			cv.put(colDeptName, "Sales");
			db.insert(deptTable, colDeptID, cv);
			cv.put(colDeptID, 2);
			cv.put(colDeptName, "IT");
			db.insert(deptTable, colDeptID, cv);
			cv.put(colDeptID, 3);
			cv.put(colDeptName, "HR");
			db.insert(deptTable, colDeptID, cv);
			db.insert(deptTable, colDeptID, cv);
			
	 }
	 
	 public String GetDept(int ID)
	 {
		 SQLiteDatabase db=this.getReadableDatabase();
		 
		 String[] params=new String[]{String.valueOf(ID)};
		 Cursor c=db.rawQuery("SELECT "+colDeptName+" FROM"+ deptTable+" WHERE "+colDeptID+"=?",params);
		 c.moveToFirst();
		 int index= c.getColumnIndex(colDeptName);
		 return c.getString(index);
	 }
	 
	 public Cursor getEmpByDept(String Dept)
	 {
		 SQLiteDatabase db=this.getReadableDatabase();
		 String [] columns=new String[]{"_id",colName,colAge,colDeptName};
		 Cursor c=db.query(viewEmps, columns, colDeptName+"=?", new String[]{Dept}, null, null, null);
		 return c;
	 }
	 
	 public int GetDeptID(String Dept)
	 {
		 SQLiteDatabase db=this.getReadableDatabase();
		 Cursor c=db.query(deptTable, new String[]{colDeptID+" as _id",colDeptName},colDeptName+"=?", new String[]{Dept}, null, null, null);
		 //Cursor c=db.rawQuery("SELECT "+colDeptID+" as _id FROM "+deptTable+" WHERE "+colDeptName+"=?", new String []{Dept});
		 c.moveToFirst();
		 return c.getInt(c.getColumnIndex("_id"));
		 
		 }
	 
	 public int UpdateEmp(Employee emp)
	 {
		 SQLiteDatabase db=this.getWritableDatabase();
		 ContentValues cv=new ContentValues();
		 cv.put(colName, emp.getName());
		 cv.put(colAge, ( emp).getAge());
		 cv.put(colDept, emp.getDept());
		 return db.update(employeeTable, cv, colID+"=?", new String []{String.valueOf(emp.getID())});
		 
	 }
	 
	 public void DeleteEmp(Employee emp)
	 {
		 SQLiteDatabase db=this.getWritableDatabase();
		 db.delete(employeeTable,colID+"=?", new String [] {String.valueOf(emp.getID())});
		 db.close();		
	 }
}

推荐答案

它告诉您代码中存在严重问题,例如无限循环或其他问题.我建议拆开调试器并逐步执行代码以遵循崩溃之前的工作.如果您必须做一些事情来使代码崩溃,那么您可能在这里也说了些什么,以帮助缩小问题的范围.但是,正如您没有看到的那样,调试器是任何人都可以给您的最佳建议.

哦,是的,张贴一个巨大的代码段并不能帮助您避免代码崩溃时所发生的事情.
It''s telling you that you''re code has a serious problem in it, like an infinite loop, or whatever. I suggest breaking out the debugger and steping through the code to follow what it''s doing just before the crash. If you have to do something to get the code to crash, you could have said something about that here too to help narrow down the problem. But, as you didn''t, the debugger is about the best advice anyone can give you.

Oh yeah, posting a giant code snippet isn''t going to help with such a non-explanation of what is happening at the time the code crashes.


这篇关于收到类似“您的应用程序意外停止,请再次尝试强制关闭"之类的错误消息.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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