2015-09-19 54 views
1

當我點擊我的字典頁面上的搜索按鈕時出現錯誤。請幫助我。android studio 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.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; 
     } 
    } 
} 

我的字典XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:id="@+id/relative" 
    android:background="@drawable/marshmellows"> 



    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Word/phrase" 
     android:id="@+id/textView9" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:textSize="25dp" 
     android:textStyle="bold" 
     android:layout_marginTop="70dp" 
     /> 

    <TextView 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Meaning" 
     android:id="@+id/textView10" 
     android:textSize="25dp" 
     android:textStyle="bold" 
     android:layout_marginTop="86dp" 

     android:layout_below="@+id/textView9" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ok" 
     android:id="@+id/Bok" 
     android:layout_alignParentBottom="true" 
     android:layout_toRightOf="@+id/textView9" 
     android:layout_toEndOf="@+id/textView9" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/etsearch" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_toLeftOf="@+id/imageButton" 
     android:layout_toStartOf="@+id/Bsearch" 
     android:layout_marginTop="20dp" 
     /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="70dp" 
     android:text="Search" 
     android:id="@+id/Bsearch" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:onClick="onSearch" 
     /> 

    <ImageButton 
     android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:id="@+id/imageButton" 
     android:src="@drawable/r" 
     android:onClick="onSpeech" 
     android:layout_toLeftOf="@+id/Bsearch" 
     android:layout_toStartOf="@+id/Bsearch" /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="70dp" 
     android:id="@+id/tvword" 
     android:layout_below="@+id/textView9" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="250dp" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:id="@+id/tvdescription" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@id/textView10"/> 

</RelativeLayout> 

我的合同類:

package com.example.pro.phord; 

/** 
* Created by Me on 13-Sep-15. 
*/ 
public class Contract { 
    String word,description; 
    public void setWord(String word) {this.word=word;} 
    public String getWord() {return this.word;} 
    public void setDescription(String description){this.description=description;} 
    public String getDescription(){return this.description;} 
} 

我logcat的說:

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) 
+1

請告訴我們什麼是錯誤? – Tauqir

+1

這是很多代碼閱讀,請張貼只有相關部分 – e4c5

+0

@Tauqir .....抱歉...新的堆棧溢出和新的android ...只是從廢品學習...我得到了答案。 ..感謝無論如何:) – rohini

回答

4

而是發佈這麼多的代碼......最初解釋什麼是你所得到的錯誤,並張貼了logcat的報告。幾個建議,進行後樣品或你在哪裏得到錯誤....通過錯誤和代碼會後反正你犯了一個簡單的錯誤代碼段後,如果裏面條件Dictionary類

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

改變它的onSearch方法到

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")); 
} 
+0

它的工作....非常感謝你..... @shadowDroid .....如果你可以我需要你告訴我還有一件事.....我如何從外部創建數據庫並將其部署到我的項目中? ....喜歡如果我創建一個約10000字的表和他們的意義,我應該從數據庫或表時,我搜索的意義...或我如何靜態插入數據庫到SQLite – rohini

+0

首先創建SQLITE DB外部你可以使用各種工具和插件。我使用Firefox中的插件來處理sqlite數據庫中的數據。對於第二個問題,您可以在資產文件夾中使用sqlite.db文件,然後您可以在應用程序中訪問它;搜索其教程。通過資產訪問數據庫是迄今爲止我看到的解決方案...但你可以做谷歌搜索你可能是其他解決方案。希望我已經回答你的問題。如果這不是你正在尋找的然後使用谷歌..對於這樣的問題使用谷歌搜索最好的解決方案發現者:) –

+0

oki非常感謝你...我會做到這一點 – rohini