E/AndroidRuntime(2886): FATAL EXCEPTION: main
E/AndroidRuntime(2886): java.lang.NullPointerException
E/AndroidRuntime(2886): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
E/AndroidRuntime(2886): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
E/AndroidRuntime(2886): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
E/AndroidRuntime(2886): at android.widget.GridView.onMeasure(GridView.java:1026)
E/AndroidRuntime(2886): at android.view.View.measure(View.java:12728)
E/AndroidRuntime(2886): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
E/AndroidRuntime(2886): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
E/AndroidRuntime(2886): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1017)
E/AndroidRuntime(2886): at android.widget.LinearLayout.onMeasure(LinearLayout.java:555)
E/AndroidRuntime(2886): at android.view.View.measure(View.java:12728)
E/AndroidRuntime(2886): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
E/AndroidRuntime(2886): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
E/AndroidRuntime(2886): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
E/AndroidRuntime(2886): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
E/AndroidRuntime(2886): at android.view.View.measure(View.java:12728)
E/AndroidRuntime(2886): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
E/AndroidRuntime(2886): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
E/AndroidRuntime(2886): at android.view.View.measure(View.java:12728)
E/AndroidRuntime(2886): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
E/AndroidRuntime(2886): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
E/AndroidRuntime(2886): at android.view.View.measure(View.java:12728)
E/AndroidRuntime(2886): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
E/AndroidRuntime(2886): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
E/AndroidRuntime(2886): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
E/AndroidRuntime(2886): at android.view.View.measure(View.java:12728)
E/AndroidRuntime(2886): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
E/AndroidRuntime(2886): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
E/AndroidRuntime(2886): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(2886): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(2886): at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(2886): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(2886): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(2886): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(2886): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(2886): at dalvik.system.NativeStart.main(Native Method)
有Logcat
和这里(希望)相关的代码。我认为问题的起源
public class Main extends Activity {
private static final int REQUEST_CODE = 0;
List<String> custom1 = new ArrayList<String>();
List<String> custom2 = new ArrayList<String>();
List<String> custom3 = new ArrayList<String>();
String title = null;
ArrayAdapter<String> a1;
ArrayAdapter<String> a2;
ArrayAdapter<String> a3;
Button b_1;
Button b_2;
Button b_3;
NotesDataSource dataSource;
MySQLiteHelper dbHelper;
SQLiteDatabase db;
int id;
int position;
boolean newNote;
static int ADAPTER = 1;
GridView gv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String savedText = null;
Bundle extras = getIntent().getExtras();
id = extras.getInt("id");
position = extras.getInt("pos");
newNote = !extras.getBoolean("load");
dataSource = new NotesDataSource(this);
final EditText tv = (EditText)findViewById(R.id.textArea);
final EditText title_tv = (EditText)findViewById(R.id.noteTitle);
if(!newNote){
savedText = extras.getString("textData");
title = extras.getString("title");
if(savedText.length() != 0){
tv.append(savedText);
}
if(title.length() != 0){
title_tv.append(title);
}
}
if(dataSource.getArray1() != null){
custom1.addAll(dataSource.getArray1());
}
else{
custom1.addAll(Arrays.asList(getResources().getStringArray(R.array.SymbolArray)));
}
if(dataSource.getArray2() != null){
custom2.addAll(dataSource.getArray2());
}
else{
custom2.addAll(Arrays.asList(getResources().getStringArray(R.array.SymbolArray)));
}
if(dataSource.getArray3() != null){
custom3.addAll(dataSource.getArray3());
}
else{
custom3.addAll(Arrays.asList(getResources().getStringArray(R.array.SymbolArray)));
}
a1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,custom1);
a2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,custom2);
a3 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,custom3);
gv = (GridView)findViewById(R.id.gridView);
gv.setAdapter(a1);
ADAPTER = 1;
gv.isFocusable();
gv.isFocusableInTouchMode();
gv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
switch(ADAPTER){
case 1:
tv.append(custom1.get(position));
break;
case 2:
tv.append(custom2.get(position));
break;
case 3:
tv.append(custom3.get(position));
default:
break;
}
}
});
b_1 = (Button)findViewById(R.id.button1);
b_2 = (Button)findViewById(R.id.button2);
b_3 = (Button)findViewById(R.id.button3);
Button b_save = (Button)findViewById(R.id.save_button);
b_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gv.setAdapter(a1);
ADAPTER = 1;
}
});
b_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gv.setAdapter(a2);
ADAPTER = 2;
}
});
b_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gv.setAdapter(a3);
ADAPTER = 3;
}
});
b_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(newNote == false){
dataSource.open();
String text = tv.getText().toString();
if(title.length() != 0){
title = title_tv.getText().toString();
dataSource.updateString(text,title,id);
}
else{
dataSource.updateString(text, "no title", id);
}
dataSource.close();
}
else{
dataSource.open();
String text = tv.getText().toString();
title = title_tv.getText().toString();
dataSource.createString(text, title);
newNote = false;
dataSource.close();
}
}
});
}
这是三种数组方法
public List<String> getArray1(){
ArrayList<String> array1 = new ArrayList<String>();
open();
Cursor cursor = db.query(MySQLiteHelper.TABLE_ARRAY, new String[]{MySQLiteHelper.COLUMN_ARRAY_1}, null, null, null, null, null);
cursor.moveToFirst();
while(!cursor.isAfterLast()){
String data = cursorToArrayString(cursor);
array1.add(data);
cursor.moveToNext();
}
//make sure to close the cursor
close();
return array1;
}
public List<String> getArray2(){
ArrayList<String> array2 = new ArrayList<String>();
open();
Cursor cursor = db.query(MySQLiteHelper.TABLE_ARRAY, new String[]{MySQLiteHelper.COLUMN_ARRAY_2}, null,null,null,null,null);
cursor.moveToFirst();
while(!cursor.isAfterLast()){
String data = cursorToArrayString(cursor);
array2.add(data);
cursor.moveToNext();
}
//make sure to close the cursor
close();
return array2;
}
public List<String> getArray3(){
ArrayList<String> array3 = new ArrayList<String>();
open();
Cursor cursor = db.query(MySQLiteHelper.TABLE_ARRAY, new String[]{MySQLiteHelper.COLUMN_ARRAY_3}, null,null,null,null,null);
cursor.moveToFirst();
while(!cursor.isAfterLast()){
String data = cursorToArrayString(cursor);
array3.add(data);
cursor.moveToNext();
}
//make sure to close the cursor
close();
return array3;
}
and finally how they're updated
public void updateArray(String[] array1, String[] array2, String[] array3){
db.delete(MySQLiteHelper.TABLE_ARRAY, null, null);
ContentValues values1 = new ContentValues();
for(int i = 0; i < array1.length; i++){
values1.put(MySQLiteHelper.COLUMN_ARRAY_1, array1[i]);
int rows = db.update(MySQLiteHelper.TABLE_ARRAY, values1, MySQLiteHelper.COLUMN_ID + " = " + i, null);
if(rows == 0){
db.insert(MySQLiteHelper.TABLE_ARRAY, null, values1);
}
}
ContentValues values2 = new ContentValues();
for(int i = 0; i < array2.length; i++){
values2.put(MySQLiteHelper.COLUMN_ARRAY_2, array2[i]);
int rows = db.update(MySQLiteHelper.TABLE_ARRAY, values2, MySQLiteHelper.COLUMN_ID + " = " + i, null);
if(rows == 0){
db.insert(MySQLiteHelper.TABLE_ARRAY, null, values2);
}
}
ContentValues values3 = new ContentValues();
for(int i = 0; i < array3.length; i++){
values3.put(MySQLiteHelper.COLUMN_ARRAY_3, array3[i]);
int rows = db.update(MySQLiteHelper.TABLE_ARRAY, values3, MySQLiteHelper.COLUMN_ID + " = " + i, null);
if(rows == 0){
db.insert(MySQLiteHelper.TABLE_ARRAY, null, values3);
}
}
}
对我来说很容易,这是我第一次使用数据库,但我很想知道问题是什么,我几天来一直在研究这个问题
答案 0 :(得分:0)
异常何时发生?在屏幕上加载?点击一下?
我只需运行一个调试器并在创建适配器实例之前放置一个断点。确保数组中的所有字符串都不为null。那是我的猜测。