Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android studio sqlite异常_Android_Database_Sqlite - Fatal编程技术网

android studio sqlite异常

android studio sqlite异常,android,database,sqlite,Android,Database,Sqlite,当我点击字典页面上的“搜索”按钮时,出现了一个错误。请帮帮我 我的数据库助手类是: package com.example.pro.phord; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sql

当我点击字典页面上的“搜索”按钮时,出现了一个错误。请帮帮我

我的数据库助手类是:

package com.example.pro.phord;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by me on 4/2/2015.
 */
public class wordshelper extends SQLiteOpenHelper{
    private static final int version = 1;
    private static final String DATABASE = "phords.db";
    private static final String TABLE= "words";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_WORD = "word";
    private static final String COLUMN_DESCRIPTION = "description";
    SQLiteDatabase db;


    private static final String TABLE_CREAT = "create table words(id integer not null , " +
            "word text primary key not null , description text not null);";

    public wordshelper(Context context) {
        super(context, DATABASE, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLE_CREAT);
        this.db = db;
    }

    public void insertContract(Contract c) {
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        String query = "select * from words";

        Cursor cursor = db.rawQuery(query , null);
        int count = cursor.getCount();

        values.put(COLUMN_ID , count);
        values.put(COLUMN_WORD, c.getWord());
        values.put(COLUMN_DESCRIPTION, c.getDescription());
        db.insert(TABLE, null, values);
        db.close();
    }
    public Cursor getMeaning(String search_word,SQLiteDatabase sqLiteDatabase){

        String[] projections = {COLUMN_WORD,COLUMN_DESCRIPTION};
        String selection = COLUMN_WORD + " LIKE ?";
        String[] selection_args = {search_word};
        Cursor cursor = sqLiteDatabase.query(TABLE,projections,selection,selection_args,null,null,null);
        return cursor;

    }


    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
        String query = "DROP TABLE IF EXISTS "+TABLE;
        db.execSQL(query);
        this.onCreate(db);

    }

}
我的字典课:

package com.example.pro.phord;

import android.app.ActionBar;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Locale;

/**
 * Created by Me on 27-Aug-15.
 */
public class Dictionary extends Activity {
    EditText searches;
    TextView words,descriptions;
    private EditText resultText;
    SQLiteDatabase sql;
    wordshelper wordhelp = new wordshelper(this);
    String search_word;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dictionary);
        searches = (EditText)findViewById(R.id.etsearch);
        words = (TextView)findViewById(R.id.tvword);
        descriptions = (TextView)findViewById(R.id.tvdescription);
        resultText=(EditText)findViewById(R.id.etsearch);
    }
    public void onSearch(View v)
    {
        search_word = searches.getText().toString();
        wordhelp = new wordshelper(getApplicationContext());
        sql = wordhelp.getReadableDatabase();
        Cursor cursor = wordhelp.getMeaning(search_word,sql);
        if (cursor.moveToFirst())
        {
            String WORD = getString(0);
            String DESCRIPTION = getString(1);
            words.setText(WORD);
            descriptions.setText(DESCRIPTION);
        }
    }
 public void onSpeech(View v)
 {
     if (v.getId()==R.id.imageButton)
     {
         EditText result=(EditText)findViewById(R.id.etsearch);
         promptSpeechInput();
     }
 }
    public void promptSpeechInput(){
        Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        i.putExtra(RecognizerIntent.EXTRA_PROMPT, "say something");
        try {
            startActivityForResult(i, 100);
        }
        catch (ActivityNotFoundException a)
        {
            Toast.makeText(Dictionary.this,"sorry! your device doesn't support speech",Toast.LENGTH_LONG).show();
        }
    }

    public void onActivityResult(int request_code,int result_code,Intent i )
    {
        super.onActivityResult(request_code,result_code,i);

        switch (request_code) {
            case 100:
                if (result_code == RESULT_OK && i !=null)
                {
                    ArrayList<String> result = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    resultText.setText(result.get(0));
                }
                break;
        }
    }
}
我的日志说:

09-19 07:53:46.549    2325-2325/com.example.pro.phord E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.pro.phord, PID: 2325
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4012)
            at android.view.View.performClick(View.java:4761)
            at android.view.View$PerformClick.run(View.java:19767)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4761)
            at android.view.View$PerformClick.run(View.java:19767)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
     Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
            at android.content.res.Resources.getText(Resources.java:274)
            at android.content.res.Resources.getString(Resources.java:360)
            at android.content.Context.getString(Context.java:376)
            at com.example.pro.phord.Dictionary.onSearch(Dictionary.java:47)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4761)
            at android.view.View$PerformClick.run(View.java:19767)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

而不是发布这么多的代码…首先解释您得到的错误是什么,然后发布logcat报告。在提出一些建议之后,要么发布示例,要么发布代码中出现错误的部分……总之,在经历了错误和代码之后,在Dictionary类的onSearch方法中的if条件中犯了一个简单的错误

if (cursor.moveToFirst())
        {
            String WORD = getString(0);
            String DESCRIPTION = getString(1);
  //remaining code
 }
换成

if (cursor.moveToFirst())
        {
            String WORD = cursor.getString(0);
            String DESCRIPTION = cursor.getString(1);
  //remaining code
 }
由于直接使用getString,它正在资源文件中查找

另一个建议是使用列索引,例如

if (cursor.moveToFirst()) {
    str = cursor.getString(cursor.getColumnIndex("content"));
}

而不是发布这么多的代码…首先解释您得到的错误是什么,然后发布logcat报告。在提出一些建议之后,要么发布示例,要么发布代码中出现错误的部分……总之,在经历了错误和代码之后,在Dictionary类的onSearch方法中的if条件中犯了一个简单的错误

if (cursor.moveToFirst())
        {
            String WORD = getString(0);
            String DESCRIPTION = getString(1);
  //remaining code
 }
换成

if (cursor.moveToFirst())
        {
            String WORD = cursor.getString(0);
            String DESCRIPTION = cursor.getString(1);
  //remaining code
 }
由于直接使用getString,它正在资源文件中查找

另一个建议是使用列索引,例如

if (cursor.moveToFirst()) {
    str = cursor.getString(cursor.getColumnIndex("content"));
}

请告诉我们错误是什么?需要阅读的代码太多,请只发布相关内容section@Tauqir ..... 很抱歉新的堆栈溢出和新的android。。。只是从废品中学习。。。我得到了答案。。。无论如何谢谢:)@e4c5对不起。。。新的一切…请告诉我们什么是错误?这是很多代码阅读,请只张贴相关的section@Tauqir ..... 很抱歉新的堆栈溢出和新的android。。。只是从废品中学习。。。我得到了答案。。。无论如何谢谢:)@e4c5对不起。。。一切都是新的…它起作用了。。。。非常感谢你@阴影机器人。。。。。如果可以,我需要你再告诉我一件事。。。。。如何在外部创建数据库并将其部署到项目中。。。。比如,如果我创建了一个包含10000个单词及其含义的表格,我应该在搜索时从该数据库或表格中获取其含义。。。。。或者我如何静态地将数据库插入SQLITE?首先,在外部创建SQLITE DB。您可以使用各种工具和插件。我在Firefox中使用插件来操作SQLITE DB中的数据。对于第二个问题,您可以使用assets文件夹中的sqlite.db文件,从那里您可以在应用程序中访问它;搜索它的教程。到目前为止,通过资产访问数据库是我看到的解决方案……但是你可以使用谷歌搜索,你可能需要其他解决方案。希望我已经回答了你的问题。如果这不是你想要的,那就用谷歌吧。。对于这些问题,请使用谷歌搜索最佳解决方案查找工具:)它奏效了。。。。非常感谢你@阴影机器人。。。。。如果可以,我需要你再告诉我一件事。。。。。如何在外部创建数据库并将其部署到项目中。。。。比如,如果我创建了一个包含10000个单词及其含义的表格,我应该在搜索时从该数据库或表格中获取其含义。。。。。或者我如何静态地将数据库插入SQLITE?首先,在外部创建SQLITE DB。您可以使用各种工具和插件。我在Firefox中使用插件来操作SQLITE DB中的数据。对于第二个问题,您可以使用assets文件夹中的sqlite.db文件,从那里您可以在应用程序中访问它;搜索它的教程。到目前为止,通过资产访问数据库是我看到的解决方案……但是你可以使用谷歌搜索,你可能需要其他解决方案。希望我已经回答了你的问题。如果这不是你想要的,那就用谷歌吧。。对于此类问题,请使用GOOGLE SEARCH best solution finder:)