从sqlite数据库检索数据时出现NullPointerException

时间:2014-11-05 12:26:54

标签: android sqlite nullpointerexception universal-image-loader

我已经在sqlite数据库中存储了一些图像名称。我的代码应该检索所有图像名并将其存储在一个数组中,然后根据其名称将第一个图像分配给一个imageview。我使用UIL将图像设置为图像视图。

我的代码

public class Locker extends Activity {
int index = 0, limit, tolerance = 40, state = 1;
String[] bgimg;
String[] ranimg;
int[] ActXCod;
int[] ActYCod;
ImageView image;
ImageLoader imageloader;
File Path;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.locker);
    image = (ImageView) findViewById(R.id.imageView1);
    SharedPreferences getprefs = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    String temp = getprefs.getString("ImageSeqNo", "4");
    limit = Integer.parseInt(temp);
    Path = getExternalFilesDir(null);
    Constants cons = new Constants();
    ranimg = cons.getImagePath(Path.toString());
    imageloader = ImageLoader.getInstance();
    try {
        new Thread(new getData()).start();

        imageloader.displayImage("file://" + Path.toString() + "/"
                + bgimg[index], image);
    } catch (Exception e) {
        String error = e.toString();
        Dialog d = new Dialog(this);
        d.setTitle("ERROR");
        TextView tv = new TextView(this);
        tv.setText(error);
        d.setContentView(tv);
        d.show();
    }

}

public boolean onTouchEvent(MotionEvent event) {
    // MotionEvent object holds X-Y values
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        int XCood = (int) event.getX();
        int YCood = (int) event.getY();
        int XMin = ActXCod[index] - tolerance, XMax = ActXCod[index]
                + tolerance, YMin = ActYCod[index] - tolerance, YMax = ActYCod[index]
                + tolerance;

        /*
         * String text = "You clicked at x = " + XCood + " and y = " +
         * YCood; final Toast toast = Toast.makeText(this, text,
         * Toast.LENGTH_SHORT); toast.show(); Handler handler = new
         * Handler(); handler.postDelayed(new Runnable() {
         * 
         * @Override public void run() { toast.cancel(); } }, 750);
         */

        if (index < (limit - 1)) // loop to check number of images
        {
            // loop to check status
            if ((state == 1)
                    && (((XCood > XMin) && (XCood < XMax)) && ((YCood > YMin) && (YCood < YMax)))) {
                index++;
                imageloader.displayImage("file://" + Path.toString() + "/"
                        + bgimg[index], image);
            } else {
                index++;
                Random r = new Random();
                int ran = r.nextInt(10 - 0);
                imageloader.displayImage("file://" + ranimg[ran], image);
                state = 0;
            }
        } else {
            if (state == 1) {
                Intent i = new Intent(Locker.this, Compare_Pattern.class);
                startActivity(i);
                finish();
            } else {
                new AlertDialog.Builder(this)
                        .setTitle("Failed Login Attempt")
                        .setMessage(
                                "Your previous login attempt has failed.Would you like to try again?")
                        .setPositiveButton(android.R.string.yes,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                        // continue with delete
                                        Intent intent = getIntent();

                                        startActivity(intent);
                                        finish();
                                    }
                                })
                        .setNegativeButton(android.R.string.no,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                        // do nothing
                                        finish();
                                    }
                                }).show();
            }
        }

    }

    return super.onTouchEvent(event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

class getData implements Runnable {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        DataBaseHandler handler = new DataBaseHandler(
                getApplicationContext());
        handler.open();
        ActXCod = handler.getXcod();
        ActYCod = handler.getYcod();
        bgimg = handler.getImg();
        handler.close();

    }

}

}

我尝试调试代码,并在行bgimg[index]imageloader.displayImage("file://" + Path.toString() + "/" + bgimg[index], image);提供 Java.lang.NullPointerException 。我无法弄清楚为什么。感谢任何帮助。

1 个答案:

答案 0 :(得分:-1)

new Thread(new getData()).start();
imageloader.displayImage("file://" + Path.toString() + "/"
            + bgimg[index], image);

bgimg尚未初始化。即使您启动了线程,它也不会立即运行并完成bgimg的初始化。

将需要线程代码结果的代码移动到线程本身。您可以使用Activity.runOnUiThread()将执行从后台线程移回UI线程。