setAdapter()崩溃Listview

时间:2018-02-19 03:23:38

标签: android

我在这里感到很沮丧,我正在尝试收集一个查询以显示在ListView上,我一步一步地看到它崩溃的地方,当我把setAdapter方法放在我的代码上时,我就去了我所看到的任何地方都需要帮助,实际上并没有让我脱离我的想法。

这是代码

public class Lista extends AppCompatActivity {
String[] nombres ={""};
private ListView contactos;


@Override
protected void onCreate(Bundle savedInstanceState) {

    try {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lista);
        contactos = (ListView)findViewById(R.id.listContact);

        Intent pikachu = new Intent(this, Main2ActivityDatos.class);

            SQLClass admin = new SQLClass(this, "contactos", null, 1);
            SQLiteDatabase db = admin.getWritableDatabase();
            Cursor fila = db.rawQuery("SELECT * FROM agenda", null);
            if(fila == null){
                Toast.makeText(this, "BD vacía", Toast.LENGTH_SHORT).show();
            }else {
                int cuenta = fila.getCount();
                String valor[] = new String[cuenta];
                int cont = 0;

                while (fila.moveToNext()) {
                    valor[cont] = fila.getString(fila.getColumnIndex("nombre"));
                    cont++;
                }

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R
                        .layout.simple_list_item_1, valor);
                contactos.setAdapter(adapter);
            }
            db.close();
    }catch(Exception e){
    }
}

我的SQL适配器

public class SQLClass extends SQLiteOpenHelper {


public SQLClass(Context context, String name, SQLiteDatabase.CursorFactory
        factory, int version){
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("CREATE TABLE agenda(nombre text primary key, numero int, " +
            "direccion text)");
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}

错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: thecerebrate.practica16, PID: 10888
              java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:409)
                  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:371)
                  at android.widget.AbsListView.obtainView(AbsListView.java:2413)
                  at android.widget.ListView.makeAndAddView(ListView.java:1986)
                  at android.widget.ListView.fillDown(ListView.java:709)
                  at android.widget.ListView.fillFromTop(ListView.java:770)
                  at android.widget.ListView.layoutChildren(ListView.java:1749)
                  at android.widget.AbsListView.onLayout(AbsListView.java:2212)
                  at android.view.View.layout(View.java:17708)
                  at android.view.ViewGroup.layout(ViewGroup.java:5631)
                  at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1197)
                  at android.view.View.layout(View.java:17708)
                  at android.view.ViewGroup.layout(ViewGroup.java:5631)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                  at android.view.View.layout(View.java:17708)
                  at android.view.ViewGroup.layout(ViewGroup.java:5631)
                  at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
                  at android.view.View.layout(View.java:17708)
                  at android.view.ViewGroup.layout(ViewGroup.java:5631)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                  at android.view.View.layout(View.java:17708)
                  at android.view.ViewGroup.layout(ViewGroup.java:5631)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1766)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1610)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1519)
                  at android.view.View.layout(View.java:17708)
                  at android.view.ViewGroup.layout(ViewGroup.java:5631)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                  at com.android.internal.policy.DecorView.onLayout(DecorView.java:774)
                  at android.view.View.layout(View.java:17708)
                  at android.view.ViewGroup.layout(ViewGroup.java:5631)
                  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2519)
                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2234)
                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1372)
                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6782)
                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:933)
                  at android.view.Choreographer.doCallbacks(Choreographer.java:742)
                  at android.view.Choreographer.doFrame(Choreographer.java:674)
                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:919)
                  at android.os.Handler.handleCallback(Handler.java:761)
                  at android.os.Handler.dispatchMessage(Handler.java:98)
                  at android.os.Looper.loop(Looper.java:156)
                  at android.app.ActivityThread.main(ActivityThread.java:6595)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

EDIT 发现问题是我的阵列上的一个总和,已经接管了它,谢谢 ThetNaing Mizo

1 个答案:

答案 0 :(得分:1)

  

String valor [] = new String [cuenta + 1];

此行,您是否已更改为:

  

String valor [] = new String [cuent];

并测试过? (不加+1)