【问题标题】:Using CursorAdapter cause my application to crash使用 CursorAdapter 导致我的应用程序崩溃
【发布时间】:2015-10-11 18:22:39
【问题描述】:

我有两节课

  • 测试
  • MyListAdapter

MyListAdapter 是扩展 CursorAdapter 的类,而 Test 是我在其中使用 MyListAdapter 类的主要活动,我的问题是在 Test 类中使用 MyListAdapter 后,我的应用程序启动良好,但是当我想去测试时活动我的应用程序崩溃了,我也想用光标对象填充我的 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)));


    }
}

这是我的测试类的代码

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);
    }
}

这是堆栈跟踪

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)

【问题讨论】:

  • 添加堆栈跟踪以明确您的问题
  • 看亲爱的没有语法错误任何异常我如何添加堆栈跟踪
  • 嗯,你写了my application to crash,表示有未处理的异常
  • Vasyl 我已经编辑了我的代码,你现在可以检查一下吗
  • 你没有初始化SQLiteDatabase db;字段,它是null

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


【解决方案1】:

我通过使用 ArrayList、ArrayAdapter 和 Model 类解决了我的问题,任何对它的工作原理感兴趣的人都可以查看此链接。

Using an ArrayAdapter with ListView

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 2016-06-04
    • 2011-08-24
    • 2014-05-03
    • 1970-01-01
    相关资源
    最近更新 更多