使用CursorAdapter导致我的应用程序崩溃

时间:2015-10-11 18:22:39

标签: android android-listview android-sqlite android-cursoradapter

我有两个班级

  • 测试
  • MyListAdapter

MyListAdapter是扩展CursorAdapter的类,而Test是类我的主要活动,我在其中使用MyListAdapter类我的问题是在Test类中使用MyListAdapter之后我的应用程序启动很好但是当我想去Test时活动我的应用程序崩溃,我也希望用游标对象填充我的ListView但我的代码不工作没有语法错误或任何类型的异常任何想法好吗?

以下是MyListAdapter类的代码

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;




public class YourSubjectListAdapter extends CursorAdapter {



    private LayoutInflater myInflater;

    //Constructor of the class
    YourSubjectListAdapter(Context context, Cursor cursor, int flag) {
        super(context, cursor, flag);
     myInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
//
        myInflater = LayoutInflater.from(viewGroup.getContext());
        //single_row is my custom layout for showing a list item
        View retView = myInflater.inflate(R.layout.single_row, viewGroup, false);
        return retView;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        TextView tvSubName = (TextView) view.findViewById(R.id.tvListSubName);
        TextView tvSubType = (TextView) view.findViewById(R.id.tvListSubType);

        tvSubType.setText(cursor.getString(cursor.getColumnIndex(StDbOpenHelper.COLUMN_SUBTYPE)));
        tvSubName.setText(cursor.getString(cursor.getColumnIndex(StDbOpenHelper.COLUMN_SUBNAME)));


    }
}

以下是我的Test类的代码

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

//Note: I have imported all needed classes

public class Test extends AppCompatActivity {
    SQLiteDatabase db;
    private static final String[] bothColumns = {StDbOpenHelper.COLUMN_SUB,StDbOpenHelper.COLUMN_MAIN};

    StDataSource ds;
    ListView testList;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        //ds is object of my StDataSource class
        ds = new StDataSource(this);

        ds.open();

        Cursor data= db.query(StDbOpenHelper.TABLE_GOODS, bothColumns, null, null, null, null, null);// <------ here is the error according to stacktrace

        YourSubjectListAdapter myAdapter = new YourSubjectListAdapter(this, data, 0);
        testList = (ListView) findViewById(R.id.lvTestList);
        testList.setAdapter(myAdapter);
    }
}

这是Stacktrace

10-12 15:27:27.370    1019-1019/ D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
10-12 15:27:27.375    1019-1019/com.myapp.emh.testapp D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
10-12 15:27:27.380    1019-1019/com.myapp.emh.testapp D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
10-12 15:27:27.385    1019-1019/com.myapp.emh.testapp E/﹕ Device driver API match
    Device driver API version: 23
    User space API version: 23
10-12 15:27:27.385    1019-1019/com.myapp.emh.testapp E/﹕ mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct  9 21:05:57 KST 2013
10-12 15:27:27.480    1019-1019/com.myapp.emh.testapp D/OpenGLRenderer﹕ Enabling debug mode 0
10-12 15:27:28.670    1019-1019/com.myapp.emh.testapp D/dalvikvm﹕ GC_FOR_ALLOC freed 133K, 9% free 10273K/11196K, paused 17ms, total 17ms
10-12 15:27:28.695    1019-1019/com.myapp.emh.testapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
10-12 15:27:28.695    1019-1019/com.myapp.emh.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 410: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
10-12 15:27:28.700    1019-1019/com.myapp.emh.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
10-12 15:27:28.700    1019-1019/com.myapp.emh.testapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
10-12 15:27:28.700    1019-1019/com.myapp.emh.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 432: Landroid/content/res/TypedArray;.getType (I)I
10-12 15:27:28.700    1019-1019/com.myapp.emh.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
10-12 15:27:28.840    1019-1019/com.myapp.emh.testapp D/dalvikvm﹕ GC_FOR_ALLOC freed 97K, 8% free 10474K/11356K, paused 25ms, total 25ms
10-12 15:27:28.840    1019-1019/com.myapp.emh.testapp I/dalvikvm-heap﹕ Grow heap (frag case) to 12.067MB for 1127536-byte allocation
10-12 15:27:28.865    1019-1030/com.myapp.emh.testapp D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 8% free 11575K/12460K, paused 28ms, total 28ms
10-12 15:27:34.415    1019-1019/com.myapp.emh.testapp D/AbsListView﹕ Get MotionRecognitionManager
10-12 15:27:34.555    1019-1019/com.myapp.emh.testapp I/logtag﹕ Database Opened !
10-12 15:27:34.555    1019-1019/com.myapp.emh.testapp D/AndroidRuntime﹕ Shutting down VM
10-12 15:27:34.555    1019-1019/com.myapp.emh.testapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41cad700)
10-12 15:27:34.575    1019-1019/com.myapp.emh.testapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.emh.testapp/com.myapp.emh.testapp.Test}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
            at android.app.ActivityThread.access$700(ActivityThread.java:159)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.myapp.emh.testapp.Test.onCreate(Test.java:34)
            at android.app.Activity.performCreate(Activity.java:5372)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
            at android.app.ActivityThread.access$700(ActivityThread.java:159)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我通过使用ArrayList,ArrayAdapter和Model类解决了我的问题,任何对它如何工作感兴趣的人都可以检查这个链接。

Using an ArrayAdapter with ListView