有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从SQLite DB动态填充Bitmapfun图像(使用Url字符串)

我目前正在开发我的安卓应用程序,不幸的是,我走到了死胡同。 我使用的是来自Bitmapfun的样本。我的问题是:这个示例运行得很好,但我尝试了各种方法来填充Images类中包含的两个数组,并将图像提供给网格。所以每次我传递不同的参数,不同的图像将被添加到两个数组中,并显示到网格中

数据库处理程序。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;
}

图像。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",
};
}

我应该如何调用另一个方法来填充这两个数组?因为当我尝试这样做时,我总是会遇到某种异常(这取决于我是否尝试将ArrayList转换为arrays并返回,等等)

感谢您的任何帮助。我被困了一个多星期了

提前谢谢, 哈里斯

另外,我不想在这里使用数组列表,我真的需要使用一个简单的数组

编辑: 我已经测试了一些虚拟数据,并尝试从另一个活动调用我的DatabaseHandler方法,它工作正常。所以我猜问题出在别的地方

这是我的日志:

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 安卓.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 安卓.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:871) 12-14 21:23:25.753: E/AndroidRuntime(9899): at 安卓.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083) 12-14 21:23:25.753: E/AndroidRuntime(9899): at 安卓.support.v4.app.BackStackRecord.run(BackStackRecord.java:635) 12-14 21:23:25.753: E/AndroidRuntime(9899): at 安卓.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431) 12-14 21:23:25.753: E/AndroidRuntime(9899): at 安卓.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:523) 12-14 21:23:25.753: E/AndroidRuntime(9899): at 安卓.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) 12-14 21:23:25.753: E/AndroidRuntime(9899): at 安卓.app.Activity.performStart(Activity.java:3791) 12-14 21:23:25.753: E/AndroidRuntime(9899): at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:1620) 12-14 21:23:25.753: E/AndroidRuntime(9899): at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 12-14 21:23:25.753: E/AndroidRuntime(9899): at 安卓.app.ActivityThread.access$1500(ActivityThread.java:117) 12-14 21:23:25.753: E/AndroidRuntime(9899): at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 12-14 21:23:25.753: E/AndroidRuntime(9899): at 安卓.os.Handler.dispatchMessage(Handler.java:99) 12-14 21:23:25.753: E/AndroidRuntime(9899): at 安卓.os.Looper.loop(Looper.java:123) 12-14 21:23:25.753: E/AndroidRuntime(9899): at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 12-14 21:23:25.753: E/AndroidRuntime(9899): at com.安卓.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


共 (1) 个答案

  1. # 1 楼答案

    如果您试图实现BitmapFun示例,我建议您放弃整个url数组,并更改库的loadImage方法,直接获取字符串url(只需注释掉获取int和imageView的url),然后使用imageFetcher。直接在适配器的getView中加载图像(item.imageUrl,imageView)