如果E/JavaBinder:!!,可以做什么!!!活页夹事务失败!!!错误来了?
我正在构建一个字典应用程序,其中我正在使用一个现有的数据库,其中包含10万个单词。现在的问题是,当我的应用程序在我的设备上运行时,它首先是启动活动,然后是另一个活动,我有一个编辑文本视图和一个搜索按钮来搜索单词。但当我按下搜索按钮时,应用程序会挂起几秒钟,它不会移动到另一个显示该单词和含义的活动。这是我的密码: 这是启动屏幕后打开的my homeActivity.java如果E/JavaBinder:!!,可以做什么!!!活页夹事务失败!!!错误来了?,java,android,linkedhashmap,android-bundle,Java,Android,Linkedhashmap,Android Bundle,我正在构建一个字典应用程序,其中我正在使用一个现有的数据库,其中包含10万个单词。现在的问题是,当我的应用程序在我的设备上运行时,它首先是启动活动,然后是另一个活动,我有一个编辑文本视图和一个搜索按钮来搜索单词。但当我按下搜索按钮时,应用程序会挂起几秒钟,它不会移动到另一个显示该单词和含义的活动。这是我的密码: 这是启动屏幕后打开的my homeActivity.java package index1.developer.acadview.com.dictionaryapp; import
package index1.developer.acadview.com.dictionaryapp;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class homeActivity extends AppCompatActivity {
public static ArrayList<DictObjectModel> data;
EditText et;
Button b1;
DatabaseHelper db;
ArrayList<String> wordcombimelist;
ArrayList<String> meancombimelist;
LinkedHashMap<String, String> namelist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
db = new DatabaseHelper(this);
et=(EditText) findViewById(R.id.et1);
b1=(Button)findViewById(R.id.but1);
data = new ArrayList<DictObjectModel>();
fetchData();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String st=et.getText().toString();
Intent go= new Intent(homeActivity.this, wordDisplay.class);
Bundle args = new Bundle();
args.putSerializable("meaningdata", (Serializable) data);
go.putExtra("wd",st);
go.putExtra("BUNDLE",args);
startActivity(go);
}
});
}
public void fetchData() {
db = new DatabaseHelper(this);
try {
db.createDataBase();
db.openDataBase();
} catch (Exception e) {
e.printStackTrace();
}
namelist = new LinkedHashMap<>();
int ii;
SQLiteDatabase sd = db.getReadableDatabase();
Cursor cursor = sd.query("Dictionary1", null, null, null, null, null, null);
ii = cursor.getColumnIndex("word");
wordcombimelist = new ArrayList<String>();
meancombimelist = new ArrayList<String>();
while (cursor.moveToNext()) {
namelist.put(cursor.getString(ii), cursor.getString(cursor.getColumnIndex("definition")));
}
Iterator entries = namelist.entrySet().iterator();
while(entries.hasNext()) {
Map.Entry thisEntry = (Map.Entry) entries.next();
wordcombimelist.add(String.valueOf(thisEntry.getKey()));
meancombimelist.add("- " + String.valueOf(thisEntry.getValue()));
}
for (int i = 0; i < wordcombimelist.size(); i++) {
data.add(new DictObjectModel(wordcombimelist.get(i), meancombimelist.get(i)));
}
}
最后是对象模型类:
package index1.developer.acadview.com.dictionaryapp;
import java.io.Serializable;
public class DictObjectModel implements Serializable {
String word, meaning;
public DictObjectModel(String word, String meaning){
this.word=word;
this.meaning = meaning;
}
public String getWord()
{
return word;
}
public String getMeaning()
{
return meaning;
}
}
请有人帮帮我。理想情况下,您不会将整个单词列表加载到内存中,因为您可能会耗尽内存,并且您的应用程序会运行得更快。即使您坚持当前加载所有单词逻辑,也不要在活动之间传递单词列表。抓住中心位置的单词,例如some
static
字段,在该字段中两个活动都可以访问它。@Commonware我了解到通过捆绑传递大数据会导致此错误。在这里,我使用hash-map首先将我的意思与单词联系起来,然后将hash-map的值存储到DictObjectModel的自定义数组列表中,然后将其传递给bundle。你说的“中心点”我没听懂。我能做些什么来消除这个错误?“我读到这个错误,通过捆绑传递大数据会导致这个错误”——正确。“我能做些什么来消除这个错误?”--您可以按照我在前面的评论中给出的建议:不要一次加载所有单词,或者不要在活动之间传递它们。另一个解决方案是不要有两个活动,而是为您的UI做一些其他的事情,例如有一个带有两个片段的活动。@Commonware好的,我会按照指示尝试,然后我会向您寻求帮助。
package index1.developer.acadview.com.dictionaryapp;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Button;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
public class DatabaseHelper extends SQLiteOpenHelper{
//The Android's default system path of your application database.
private static String DB_PATH = "";
private static String DB_NAME = "dictionary.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to
the application assets and resources.
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
DB_PATH= myContext.getDatabasePath(DB_NAME).toString();
}
/**
* Creates a empty database on the system and rewrites it with your own
database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the
default system path
//of your application so we are gonna be able to overwrite that
database with our database.
this.getWritableDatabase();
this.close();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each
time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
// this.getReadableDatabase();
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH ;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created
empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH ;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH ;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// Add your public helper methods to access and get content from the
database.
// You could return cursors by doing "return myDataBase.query(....)" so
it'd be easy
// to you to create adapters for your views.
//add your public methods for insert, get, delete and update data in
database.
}
package index1.developer.acadview.com.dictionaryapp;
import java.io.Serializable;
public class DictObjectModel implements Serializable {
String word, meaning;
public DictObjectModel(String word, String meaning){
this.word=word;
this.meaning = meaning;
}
public String getWord()
{
return word;
}
public String getMeaning()
{
return meaning;
}
}