Dewey1994 2015-03-18 13:31 采纳率: 0%
浏览 4351

Android程序在手机上运行崩溃但是在模拟器上能运行但是还有一个模拟器也是崩溃的

package darkhorse.english.app;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.CursorAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class LookupActivity extends Activity implements OnClickListener, TextWatcher
{
private final String DATABASE_PATH = android.os.Environment
.getExternalStorageDirectory().getAbsolutePath()
+ "/dictionary";
private AutoCompleteTextView autocompletetextview;
private final String DATABASE_FILENAME = "dictionary.db";
private SQLiteDatabase database;
private ImageButton lookupbutton;
private TextView textview;
private ImageButton clearbutton;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.lookup_layout);
    database = openDatabase();
    lookupbutton = (ImageButton) findViewById(R.id.lookupbutton);
    textview = (TextView) findViewById(R.id.textview);
    clearbutton = (ImageButton) findViewById(R.id.clearbutton);
    autocompletetextview = (AutoCompleteTextView) findViewById(R.id.autocompletetextview);
    lookupbutton.setOnClickListener(this);
    autocompletetextview.addTextChangedListener(this);
    clearbutton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            autocompletetextview.setText("");
            textview.setText("");
        }
    });
}



public class DictionaryAdapter extends CursorAdapter
{
    private LayoutInflater layoutInflater;
    @Override
    public CharSequence convertToString(Cursor cursor)
    {
        return cursor == null ? "" : cursor.getString(cursor.getColumnIndex("_id"));
    }

    private void setView(View view, Cursor cursor)
    {
        TextView tvWordItem = (TextView) view;
        tvWordItem.setText(cursor.getString(cursor.getColumnIndex("_id")));
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor)
    {
        setView(view, cursor);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent)
    {
        View view = layoutInflater.inflate(R.layout.word_list_item, null);
        setView(view, cursor);
        return view;
    }

    public DictionaryAdapter(Context context, Cursor c, boolean autoRequery)
    {
        super(context, c, autoRequery);
        layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
}

public void afterTextChanged(Editable s)
{

    //  必须将English字段的别名设为_id 
    Cursor cursor = database.rawQuery(
            "select english as _id from t_words where english like ?",
            new String[]
            { s.toString() + "%" });
    DictionaryAdapter dictionaryAdapter = new DictionaryAdapter(this,
            cursor, true);
    autocompletetextview.setAdapter(dictionaryAdapter);

    }

public void beforeTextChanged(CharSequence s, int start, int count,
        int after)
{
    // TODO Auto-generated method stub

}

public void onTextChanged(CharSequence s, int start, int before, int count)
{
    // TODO Auto-generated method stub

}


public void onClick(View view)
{
    String sql = "select chinese from t_words where english=?";     
    Cursor cursor = database.rawQuery(sql, new String[]
    { autocompletetextview.getText().toString() });
    String result = "未找到该单词.";
    //  如果查找单词,显示其中文的意思
    if (cursor.getCount() > 0)
    {
        //  必须使用moveToFirst方法将记录指针移动到第1条记录的位置
        cursor.moveToFirst();
        result = cursor.getString(cursor.getColumnIndex("chinese"));
    }
    //  显示查询结果对话框
    textview.setText(result);

}


private SQLiteDatabase openDatabase()
{
    try
    {
        // 获得dictionary.db文件的绝对路径
        String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
        File dir = new File(DATABASE_PATH);
        // 如果/sdcard/dictionary目录中存在,创建这个目录
        if (!dir.exists())
            dir.mkdir();
        // 如果在/sdcard/dictionary目录中不存在
        // dictionary.db文件,则从res\raw目录中复制这个文件到
        // SD卡的目录(/sdcard/dictionary)
        if (!(new File(databaseFilename)).exists())
        {
            // 获得封装dictionary.db文件的InputStream对象
            InputStream is = getResources().openRawResource(
                    R.raw.dictionary);
            FileOutputStream fos = new FileOutputStream(databaseFilename);
            byte[] buffer = new byte[8192];
            int count = 0;
            // 开始复制dictionary.db文件
            while ((count = is.read(buffer)) > 0)
            {
                fos.write(buffer, 0, count);
            }

            fos.close();
            is.close();
        }
        // 打开/sdcard/dictionary目录中的dictionary.db文件
        SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
                databaseFilename, null);
        return database;
    }
    catch (Exception e)
    {
    }
    return null;
}

}

求高手给看看

  • 写回答

3条回答

  • zengnm 2015-03-18 14:46
    关注

    应该是Android 版本问题,你的模拟器Android版本和手机 不同。

    评论

报告相同问题?

悬赏问题

  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?