2

I Have Class G and ActivityMain. so my problem is in ActivityMain in first line of for loop I gave NullPointerException. of course in genymotion my app work correctly but in avd of eclipse......!i gave that. so can anyone help me I so be cheerful:D

public class G extends Application {

public static Context               context;
public static LayoutInflater        inflater;
public static Activity              currentActivity;
public static ArrayList<StructNote> notes        = new ArrayList<StructNote>();

public static SQLiteDatabase        database;
public static final String          DIR_SDCARD   = Environment.getExternalStorageDirectory().getAbsolutePath();
public static final String          DIR_DATABASE = DIR_SDCARD + "/database-word/";

public static String                Word;
public static String                word_mean;
public static Cursor                cursor;
public static int                   i            = 0;
public static String[]              array;

@Override
public void onCreate() {
    super.onCreate();
    context = getApplicationContext();
    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    new File(DIR_DATABASE).mkdirs();

    database = SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE + "/database.sqlite", null);
    database.execSQL("CREATE  TABLE  IF NOT EXISTS word (" +
            "word_id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , " +
            "word TEXT, " +
            "word_mean TEXT)");

    cursor = database.rawQuery("SELECT * FROM word", null);
    array = new String[cursor.getCount()];
    while (cursor.moveToNext()) {

        Word = cursor.getString(cursor.getColumnIndex("word"));
        word_mean = cursor.getString(cursor.getColumnIndex("word_mean"));
        array[i] = Word;
        i++;
    }
    cursor.close();
}

}

public class ActivityMain extends Activity {

public ArrayAdapter adapter;
public int          k         = 0;


@Override
protected void onResume() {
    G.currentActivity = this;
    super.onResume();
    if (adapter != null) {
        adapter.notifyDataSetChanged();
    }
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView lstContent = (ListView) findViewById(R.id.lstContent);

    adapter = new AdapterNote(G.notes);
    lstContent.setAdapter(adapter);

for (k = 0; k < G.Word.length(); k++) {
        StructNote note = new StructNote();
         note.title = "" +("#" + G.array[k], "#");
         G.notes.add(note);
    }
 adapter.notifyDataSetChanged();
}

}

alireza71
  • 339
  • 1
  • 3
  • 14
  • post your logcat with the error message and indicate which line in your code is the failure – Martin Apr 15 '15 at 19:39
  • I told you in first of for loop in ActivityMain.but my logcat is " E/AndroidRuntime(6896): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.course.app.test_list_view/com.myapp.course.app.test_list_view.ActivityMain}: java.lang.NullPointerException " – alireza71 Apr 15 '15 at 19:48
  • what line is that? StructNote note = new StructNote()? – Martin Apr 16 '15 at 22:18
  • solved!my app became force close because couldn't create database.sqlite in device.and nothing exist on my database path. thanks for your attention. – alireza71 Apr 17 '15 at 07:48

0 Answers0