Android 使用CursorAdapter会导致我的应用程序崩溃
我有两节课Android 使用CursorAdapter会导致我的应用程序崩溃,android,android-listview,android-sqlite,android-cursoradapter,Android,Android Listview,Android Sqlite,Android Cursoradapter,我有两节课 试验 聚酯适配器 MyListAdapter是扩展CursorAdapter的类,Test is类是我的主要活动,我在其中使用MyListAdapter类。我的问题是,在测试类中使用MyListAdapter后,我的应用程序启动良好,但当我想去测试活动时,我的应用程序崩溃,另外,我想用游标对象填充我的ListView,但我的代码不起作用,没有语法错误或任何类型的异常,知道吗 下面是MyListAdapter类的代码 import android.content.Context;
- 试验
- 聚酯适配器
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);
}
}
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入android.os.Bundle;
导入android.support.annotation.Nullable;
导入android.support.v7.app.AppActivity;
导入android.widget.ListView;
//注意:我已经导入了所有需要的类
公共类测试扩展了AppCompatActivity{
sqlitedb数据库;
私有静态最终字符串[]bothColumns={StDbOpenHelper.COLUMN_SUB,StDbOpenHelper.COLUMN_MAIN};
stds数据源;
ListView测试列表;
@凌驾
创建时受保护的void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
//ds是我的StDataSource类的对象
ds=新的数据源(此);
ds.open();
Cursor data=db.query(StDbOpenHelper.TABLE_GOODS,两个列,null,null,null,null,null);//我通过使用ArrayList,ArrayAdapter和一个模型类解决了我的问题。任何对它的工作方式感兴趣的人都可以检查这个链接
添加stacktrace以清除您的问题亲爱的没有语法错误任何类型的异常我如何添加stacktraceHm,您编写了我的应用程序崩溃
,这意味着存在未处理的异常Vasyl我编辑了我的代码您现在可以检查它吗请检查您没有初始化SQLiteDatabase db;
字段,它是null
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)