I am currently working on my android application and sadly, I came at a dead end. I am using the sample from Bitmapfun . My problem is this : The sample is working fine but I have tried every single way trying to populate the two arrays that are included in the Images class and provide the images to the grid. So every time I pass different parameters, different images will be added into the two arrays and get displayed into the grid.
DatabaseHandler.java
public String[] getImagesUrl(String rest_name, String area) {
String[] images_url = new String[] {};
// Select All Query
String selectQuery = "SELECT * FROM menu_images where _area ='" + area + "'" + " AND rest_name='" + rest_name + "'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
for (int i = 0; i < cursor.getCount(); i++)
{
images_url[i] = cursor.getString(cursor.getColumnIndex("_images"));
cursor.moveToNext();
}
}
// closing connection
cursor.close();
db.close();
return images_url;
}
Images.java
import java.util.ArrayList;
import java.util.List;
import com.example.project.DatabaseHandler;
public class Images {
private static DatabaseHandler dbHandler;
public final static String[] imageUrls = new String[] {
"http://xtcgamers.com/images/pic2.png",
};
public final static String[] imageThumbUrls = new String[] {
"http://xtcgamers.com/thumb_images/pic2.png",
};
}
How should I call my other method so I can populate those two Arrays? Because when I am trying to, I always get some kind of exception (it depends if I try to convert arraylists to arrays and back, etc.)
Any kind of help is appreciated. I've been stuck for over a week.
Thanks in advance, harris
ps. I do NOT want to use an arraylist here, I really need to use a simple array .
edit: I have tested out some dummy data and tried to call my DatabaseHandler method from another activity and it's working fine. So I guess the problem is somewhere else.
Here is my logcat :
12-14 21:23:25.753: E/AndroidRuntime(9899): FATAL EXCEPTION: main 12-14 21:23:25.753: E/AndroidRuntime(9899): java.lang.ExceptionInInitializerError 12-14 21:23:25.753: E/AndroidRuntime(9899): at com.example.project.ui.ImageGridFragment$ImageAdapter.getCount(ImageGridFragment.java:221) 12-14 21:23:25.753: E/AndroidRuntime(9899): at android.widget.GridView.setAdapter(GridView.java:131) 12-14 21:23:25.753: E/AndroidRuntime(9899): at com.example.project.ui.ImageGridFragment.onCreateView(ImageGridFragment.java:97) 12-14 21:23:25.753: E/AndroidRuntime(9899): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871) 12-14 21:23:25.753: E/AndroidRuntime(9899): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083) 12-14 21:23:25.753: E/AndroidRuntime(9899): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635) 12-14 21:23:25.753: E/AndroidRuntime(9899): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431) 12-14 21:23:25.753: E/AndroidRuntime(9899): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:523) 12-14 21:23:25.753: E/AndroidRuntime(9899): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) 12-14 21:23:25.753: E/AndroidRuntime(9899): at android.app.Activity.performStart(Activity.java:3791) 12-14 21:23:25.753: E/AndroidRuntime(9899): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1620) 12-14 21:23:25.753: E/AndroidRuntime(9899): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 12-14 21:23:25.753: E/AndroidRuntime(9899): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 12-14 21:23:25.753: E/AndroidRuntime(9899): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 12-14 21:23:25.753: E/AndroidRuntime(9899): at android.os.Handler.dispatchMessage(Handler.java:99) 12-14 21:23:25.753: E/AndroidRuntime(9899): at android.os.Looper.loop(Looper.java:123) 12-14 21:23:25.753: E/AndroidRuntime(9899): at android.app.ActivityThread.main(ActivityThread.java:3683) 12-14 21:23:25.753: E/AndroidRuntime(9899): at java.lang.reflect.Method.invokeNative(Native Method) 12-14 21:23:25.753: E/AndroidRuntime(9899): at java.lang.reflect.Method.invoke(Method.java:507) 12-14 21:23:25.753: E/AndroidRuntime(9899): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 12-14 21:23:25.753: E/AndroidRuntime(9899): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 12-14 21:23:25.753: E/AndroidRuntime(9899): at dalvik.system.NativeStart.main(Native Method) 12-14 21:23:25.753: E/AndroidRuntime(9899): Caused by: java.lang.NullPointerException 12-14 21:23:25.753: E/AndroidRuntime(9899): at com.example.project.provider.Images.(Images.java:61) 12-14 21:23:25.753: E/AndroidRuntime(9899): ... 22 more
getCount()
method herecom.example.project.ui.ImageGridFragment$ImageAdapter.getCount(ImageGridFragment.java:221)
...