当我尝试从数据库创建listView收费日期时,Android应用程序崩溃

时间:2015-02-03 12:03:32

标签: android listview

我正在开发一个基于购物清单的简单Android项目。我有两个按钮,一个将项目添加到列表I,另一个按钮向我显示列表,我使用了数据库和listview。将项添加到列表我工作正常,但显示列表时给我空指针异常。花了几天时间才看出失败。我已经执行了调试,但没有看到该行失败。我认为问题是从中读取数据 数据库。

应该打开并直接跳转异常的类:

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Mostrar extends Activity {
  ListView listado;
  Cursor consulta;
  int num = consulta.getCount();
  String [] elementos = new String [num];
  ArrayAdapter<String> adaptador;
  int i;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mostrar);
    listado = (ListView)findViewById(R.id.lista);

    //ABRIMOS LA BASE DE DATOS
    BaseDatos bd = new BaseDatos(this, "proyecto" , null, 1);
    SQLiteDatabase sq = bd.getReadableDatabase();

    //HACEMOS UNA CONSULTA
    consulta =  sq.rawQuery("SELECT productos from Lista",null);
    try{
        if (consulta.moveToFirst()){
            i = 0;
            do {
                if(consulta.getString(0) != null){
                    //cargo array con elementos que me devuelve el select
                    elementos[i]= consulta.getString(0);
                    i++;
                }

            }while (consulta.moveToNext());
        }
    }catch (Exception e){
    }
    //cerramos base de datos
    sq.close();
    adaptador = new  ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, elementos);
    listado.setAdapter(adaptador);
}
}

该类的xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<AbsoluteLayout
    android:id="@+id/AbsoluteLayout2"
    android:layout_width="wrap_content"
    android:layout_height="461dp"
    android:background="@drawable/mostrar2" >

    <ListView
        android:id="@+id/lista"
        android:layout_width="match_parent"
        android:layout_height="404dp"
        android:layout_x="16dp"
        android:layout_y="55dp" >
    </ListView>

   <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="18dp"
        android:layout_y="27dp"
        android:text="Productos:"
        android:textAppearance="?android:attr/textAppearanceLarge" /> 
</AbsoluteLayout>
</ScrollView>

带有两个按钮的主类:

   public class Principal extends ActionBarActivity { 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.principal);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.principal, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
  public void lanzarMostrar (View view){
         Intent i = new Intent(this, Mostrar.class);
         startActivity(i);  
    }
  public void lanzarModificar(View view){
         Intent i = new Intent(this, Modificar.class);
         startActivity(i);  
    }  
}

错误:

 02-03 12:56:18.172: E/AndroidRuntime(4362): FATAL EXCEPTION: main
02-03 12:56:18.172: E/AndroidRuntime(4362): java.lang.RuntimeException:  Unable to instantiate activity     ComponentInfo{com.example.proyecto/com.example.proyecto.Mostrar}:  java.lang.NullPointerException 
02-03 12:56:18.172: E/AndroidRuntime(4362):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
02-03 12:56:18.172: E/AndroidRuntime(4362):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-03 12:56:18.172: E/AndroidRuntime(4362):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-03 12:56:18.172: E/AndroidRuntime(4362):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-03 12:56:18.172: E/AndroidRuntime(4362):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 12:56:18.172: E/AndroidRuntime(4362):     at android.os.Looper.loop(Looper.java:137)
02-03 12:56:18.172: E/AndroidRuntime(4362):     at android.app.ActivityThread.main(ActivityThread.java:4464)

1 个答案:

答案 0 :(得分:1)

NPE
  int num = consulta.getCount();

因为consulta=null

解决方案:

在使用之前初始化您的Cursor咨询。

consulta =  sq.rawQuery("SELECT productos from Lista",null);
num=consulta.getCount();