简体   繁体   中英

Edittext Suggestion on android Studio

help me how to fix this code from AutocompleteTextview i cant see the error code from this part

 final String [] myData =  myDB.SelectAllData();

 final AutoCompleteTextView autoCom = (AutoCompleteTextView)findViewById(R.id.TVresult);
 final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice,myData);
    autoCom.setAdapter(adapter);

and This Part

public String[] SelectAllData() {


    try {
        String ArrayData[] = null;
        SQLiteDatabase db;
        db = this.getReadableDatabase(); // Read Data

        String strSQL = "SELECT  * FROM " + DB_TABLE_NAME;
        Cursor cursor = db.rawQuery(strSQL, null);

        if(cursor != null)
        {
            if (cursor.moveToFirst()) {
                ArrayData = new String[cursor.getCount()];
                /***
                 *  [x] = Name
                 */
                int i= 0;
                do {
                    ArrayData[i] = cursor.getString(0);
                    i++;
                } while (cursor.moveToNext());

            }
        }
        cursor.close();

        return ArrayData;

    } catch (Exception e) {
        return null;
    }

}

This is my code of MainActivity

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    resulttext = (TextView) findViewById(R.id.TVresult);
    rescebu = (TextView) findViewById(R.id.english);
    trans = (Button) findViewById(R.id.translate);
    respam = (TextView) findViewById(R.id.tagalog);
    resilo = (TextView) findViewById(R.id.vis);
    resbik = (TextView) findViewById(R.id.ilonngo);
    myDB = new DatabaHelper(this);
    tts = new TextToSpeech(this, this);
    final String [] myData =  myDB.SelectAllData();

    final AutoCompleteTextView autoCom = (AutoCompleteTextView)findViewById(R.id.TVresult);
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice,myData);
    autoCom.setAdapter(adapter);
    autoCom.setThreshold(1);
    autoCom.setTextColor(Color.RED);

This is The Dabasehelper and SelectAllData Method

public class DatabaHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "translator4.sqlite";
private static final String DB_TABLE_NAME = "wews";
public DatabaHelper(Context context) {

    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

}

public Cursor getAllData(String English, String Tagalog, String Visaya, String Ilonggo) {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from wews where English like '" + English + "' or Tagalog like '" + Tagalog + "' or Visayan like '" + Visaya + "' or Ilonggo like '" + Ilonggo + "';", null);

    return res;
}


@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}


public String[] SelectAllData() {


    try {
        String ArrayData[] = null;
        SQLiteDatabase db;
        db = this.getReadableDatabase(); // Read Data

        String strSQL = "SELECT  * FROM " + DB_TABLE_NAME;
        Cursor cursor = db.rawQuery(strSQL, null);

        if(cursor != null)
        {
            if (cursor.moveToFirst()) {
                ArrayData = new String[cursor.getCount()];
                /***
                 *  [x] = Name
                 */
                int i= 0;
                do {
                    ArrayData[i] = cursor.getString(0);
                    i++;
                } while (cursor.moveToNext());

            }
        }
        cursor.close();

        return ArrayData;

    } catch (Exception e) {
        return null;
    }

}

This is The logs LogCat

Process: application.example.com.myapplication, PID: 20663
                                                                                           Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
                                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{application.example.com.myapplication/application.example.com.myapplication.MainActivity}: java.lang.NullPointerException: storage == null
                                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462)
                                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)
                                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
                                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5475)
                                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                               at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
                                                                                            Caused by: java.lang.NullPointerException: storage == null
                                                                                               at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
                                                                                               at java.util.Arrays.asList(Arrays.java:155)
                                                                                               at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:137)
                                                                                               at application.example.com.myapplication.MainActivity.onCreate(MainActivity.java:56)
                                                                                               at android.app.Activity.performCreate(Activity.java:7125)
                                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2415)
                                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522) 
                                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
                                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5475) 
                                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                                               at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)

Possible Modification in this code block as you missed to created string object ArrayData[i] = new String() in do...while loop.

public String[] SelectAllData() {


    try { 
        String ArrayData[] = null;
        SQLiteDatabase db;
        db = this.getReadableDatabase(); // Read Data

        String strSQL = "SELECT  * FROM " + DB_TABLE_NAME;
        Cursor cursor = db.rawQuery(strSQL, null);

        if(cursor != null)
        { 
            if (cursor.moveToFirst()) {
                ArrayData = new String[cursor.getCount()];
                /*** 
                 *  [x] = Name 
                 */ 
                int i= 0;
                do { 
                    ArrayData[i] = new String();
                    ArrayData[i] = cursor.getString(0);
                    i++;
                } while (cursor.moveToNext());

            } 
        } 
        cursor.close();

        return ArrayData;

    } catch (Exception e) {
        return null; 
    } 

}

I'd suggest changing :-

public String[] SelectAllData() {


    try {
        String ArrayData[] = null;
        SQLiteDatabase db;
        db = this.getReadableDatabase(); // Read Data

        String strSQL = "SELECT  * FROM " + DB_TABLE_NAME;
        Cursor cursor = db.rawQuery(strSQL, null);

        if(cursor != null)
        {
            if (cursor.moveToFirst()) {
                ArrayData = new String[cursor.getCount()];
                /***
                 *  [x] = Name
                 */
                int i= 0;
                do {
                    ArrayData[i] = cursor.getString(0);
                    i++;
                } while (cursor.moveToNext());

            }
        }
        cursor.close();

        return ArrayData;

    } catch (Exception e) {
        return null;
    }

}

to :-

public String[] SelectAllData() {

    try {
        String ArrayData[];
        SQLiteDatabase db;
        db = this.getReadableDatabase(); // Read Data

        String strSQL = "SELECT  * FROM " + DB_TABLE_NAME;
        Cursor cursor = db.rawQuery(strSQL, null);

        ArrayData = new String[cursor.getCount()];
        int i = 0;
        while (cursor.moveToNext()) {
            ArrayData[i++] = cursor.getString(0);
            //ArrayData[cursor.getPosition()] = cursor.getString(0); //Could be an alternative
        }
        cursor.close();

        return ArrayData;

    } catch (Exception e) {
        return new String[0];
    }
}

In short, the check for a null cursor is useless it DOES NOT signify no data, rather an empty cursor with a count of 0 is returned when there is no data.

Thus when there is no data cursor.moveToFirst will be false and none of the code within the if will be executed. As such ArrayData will be null ( the likely reason for the null pointer exception ).

The modified code will return an empty array (0 elements) if there is no data or if there is another exception captured in the try block.

You could also do away with using a counter/index, by using cursor.getPosition() , which is effectively the same (as per commented out line).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM