gpt4 book ai didi

java - 为什么无法向sqlite中插入数据?

转载 作者:行者123 更新时间:2023-12-01 15:51:16 25 4
gpt4 key购买 nike

我不知道为什么它返回空指针异常。我认为是因为该表没有数据。所以,数据似乎没有添加到数据库中

这是我的 Activity 课。

private void createdata(){

String title[] = {"rancangan1", "rancangan2", "rancangan3", "rancangan4", "rancangan5"};
String date[] = {"isnin", "selasa", "rabu", "khamis", "jumaat"};
String time[] = {"17:00-18:00", "18:00-19:00", "17:00-18:00", "18:00-19:00", "19:00-20:00"};
String channel[] = {"astro ria", "astro ria", "astro ria", "astro ria", "astro ria"};
try{
for(int i = 0; i < title.length; i++){
ContentValues values = new ContentValues();
values.put(ImamShareData.DataContent.KEY_PROGRAM_TITLE, title[i]);
values.put(ImamShareData.DataContent.KEY_PROGRAM_DATE, date[i]);
values.put(ImamShareData.DataContent.KEY_PROGRAM_TIME, time[i]);
values.put(ImamShareData.DataContent.KEY_PROGRAM_CHANNEL, channel[i]);
cr.insert(ImamShareData.DataContent.PROGRAM_URI, values);
//Log.i(TAG, "Successfully added index " + i + " as ID " + mId[i]);
}
}catch(Exception e){
Log.v("error",""+e);
}

}
private void filldata(){

String[] from = new String[]{ImamShareData.DataContent.KEY_PROGRAM_DATE, ImamShareData.DataContent.KEY_PROGRAM_TIME, ImamShareData.DataContent.KEY_PROGRAM_CHANNEL};
String[] from1 = new String[]{ImamShareData.DataContent.KEY_PROGRAM_TITLE};

int[] to = new int[]{R.id.programdate, R.id.programtime, R.id.programchannel};
int[] to1 = new int[]{R.id.programtitle};

SimpleExpandableListAdapter SEL = new SimpleExpandableListAdapter(this,
createGroupList(), R.layout.programgroup_row, from1, to1,
createChildList(), R.layout.programchild_row, from, to);
setListAdapter( SEL );
}
private List createGroupList() {
String[] column = new String[]{ImamShareData.DataContent.KEY_PROGRAM_TITLE};
ArrayList result = new ArrayList();
String title[] = null;
for( int i = 1 ; i <= 5; ++i ) {
Cursor cursor = managedQuery(ImamShareData.DataContent.PROGRAM_URI, column, null, null, null);
HashMap m = new HashMap();
title[i] = cursor.getColumnName(cursor.getColumnIndex(ImamShareData.DataContent.KEY_PROGRAM_TITLE));
m.put(ImamShareData.DataContent.KEY_PROGRAM_TITLE, title[i]);
result.add( m );
}
return result;
}

private List createChildList() {
String date;
String time;
String channel;
String[] column = new String[]{ImamShareData.DataContent.KEY_PROGRAM_DATE, ImamShareData.DataContent.KEY_PROGRAM_TIME, ImamShareData.DataContent.KEY_PROGRAM_CHANNEL};
ArrayList result = new ArrayList();
for( int i = 1 ; i <= 5 ; ++i ) {
ArrayList secList = new ArrayList();
for( int n = 0 ; n < 3 ; n+=3 ) {
Cursor cursor = managedQuery(ImamShareData.DataContent.PROGRAM_URI, column, null, null, null);
HashMap child = new HashMap();
date = cursor.getColumnName(cursor.getColumnIndex(ImamShareData.DataContent.KEY_PROGRAM_DATE));
time = cursor.getColumnName(cursor.getColumnIndex(ImamShareData.DataContent.KEY_PROGRAM_TIME));
channel = cursor.getColumnName(cursor.getColumnIndex(ImamShareData.DataContent.KEY_PROGRAM_CHANNEL));
child.put(ImamShareData.DataContent.KEY_PROGRAM_DATE, date);
child.put(ImamShareData.DataContent.KEY_PROGRAM_TIME, time);
child.put(ImamShareData.DataContent.KEY_PROGRAM_CHANNEL, channel);
secList.add( child );
}
result.add( secList );
}
return result;
}

这是我的内容提供者类

@Override
public Uri insert(Uri uri, ContentValues initialvalues) {
TableNumber = sUriMatcher.match(uri);
if( TableNumber != PROGRAM){
throw new IllegalArgumentException("Unknown URI " + uri);
}

ContentValues values;
if(initialvalues != null){
values = new ContentValues(initialvalues);
}else{
values = new ContentValues();
}

SQLiteDatabase mDb = mDbHelper.getWritableDatabase();
long rowId = mDb.insert(DatabaseHelper.TABLE_PROGRAM, null, values);
if(rowId > 0){
Uri programUri = ContentUris.withAppendedId(ImamShareData.DataContent.PROGRAM_URI, rowId);
getContext().getContentResolver().notifyChange(programUri, null);
return programUri;
}
throw new IllegalArgumentException("Failed to insert row into " + uri);
}

这是我的数据共享类(class)

public static final class DataContent implements BaseColumns{   

public static final Uri PROGRAM_URI = Uri.parse("content://" + AUTHORITY + "/" + PROGRAMPATH);

public static final String CONTENT_MORE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.android.imammuda";
public static final String CONTENT_ONE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.android.imammuda";

public static final String KEY_PROGRAM_ID = "_id";
public static final String KEY_PROGRAM_TITLE = "ProgramTitle";
public static final String KEY_PROGRAM_DATE = "ProgramDate";
public static final String KEY_PROGRAM_TIME = "ProgramTime";
public static final String KEY_PROGRAM_CHANNEL = "ProgramChannel";
}

这是我的日志猫

05-12 16:05:46.024: ERROR/AndroidRuntime(5175): Caused by: java.lang.NullPointerException
05-12 16:05:46.024: ERROR/AndroidRuntime(5175): at com.android.imammuda.Program.createGroupList(Program.java:112)
05-12 16:05:46.024: ERROR/AndroidRuntime(5175): at com.android.imammuda.Program.filldata(Program.java:82)

最佳答案

您尚未在 createGroupList 方法中初始化标题变量:

 String title[] = null;

...

title[i] = cursor.getColumnName....

在 for 循环中,长度被硬编码为最大 5,因此最简单的修复方法是:

String[] title = new String[5];

关于java - 为什么无法向sqlite中插入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5975578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com