启动我的andriod应用程序时出现以下错误。这是一个巨大的数据库:
12-08 21:35:01.158: W/dalvikvm(12461): threadid=1: thread exiting with uncaught exception (group=0x417fdac8)
12-08 21:35:01.158: E/AndroidRuntime(12461): FATAL EXCEPTION: main
12-08 21:35:01.158: E/AndroidRuntime(12461): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pocketbotanist/com.pocketbotanist.HomeScreen}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
12-08 21:35:01.158: E/AndroidRuntime(12461): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
12-08 21:35:01.158: E/AndroidRuntime(12461): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
12-08 21:35:01.158: E/AndroidRuntime(12461): at android.app.ActivityThread.access$700(ActivityThread.java:152)
12-08 21:35:01.158: E/AndroidRuntime(12461): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
12-08 21:35:01.158: E/AndroidRuntime(12461): at android.os.Handler.dispatchMessage(Handler.java:99)
12-08 21:35:01.158: E/AndroidRuntime(12461): at android.os.Looper.loop(Looper.java:137)
12-08 21:35:01.158: E/AndroidRuntime(12461): at android.app.ActivityThread.main(ActivityThread.java:5328)
12-08 21:35:01.158: E/AndroidRuntime(12461): at java.lang.reflect.Method.invokeNative(Native Method)
12-08 21:35:01.158: E/AndroidRuntime(12461): at java.lang.reflect.Method.invoke(Method.java:511)
12-08 21:35:01.158: E/AndroidRuntime(12461): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
12-08 21:35:01.158: E/AndroidRuntime(12461): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12-08 21:35:01.158: E/AndroidRuntime(12461): at dalvik.system.NativeStart.main(Native Method)
12-08 21:35:01.158: E/AndroidRuntime(12461): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
12-08 21:35:01.158: E/AndroidRuntime(12461): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
12-08 21:35:01.158: E/AndroidRuntime(12461): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
12-08 21:35:01.158: E/AndroidRuntime(12461): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
12-08 21:35:01.158: E/AndroidRuntime(12461): at android.database.CursorWrapper.getString(CursorWrapper.java:114)
12-08 21:35:01.158: E/AndroidRuntime(12461): at com.pocketbotanist.HomeScreen.onCreate(HomeScreen.java:72)
12-08 21:35:01.158: E/AndroidRuntime(12461): at android.app.Activity.performCreate(Activity.java:5250)
12-08 21:35:01.158: E/AndroidRuntime(12461): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
12-08 21:35:01.158: E/AndroidRuntime(12461): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-08 21:35:01.158: E/AndroidRuntime(12461): ... 11 more
这是主屏幕:
package com.pocketbotanist;
import java.io.File;
import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import com.pocketbotanist.contentprovider.MyEntryContentProvider;
import com.pocketbotanist.database.EntryTable;
public class HomeScreen extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int DELETE_ID = Menu.FIRST + 1;
// private Cursor cursor;
private SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
this.getListView().setDividerHeight(2);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//Initial folder creation code
File appDirectory = new File(Environment.getExternalStorageDirectory().toString()+"/Pocket Botanist/");
if (!appDirectory.exists()){
appDirectory.mkdir();
}
//Drop down list code
Spinner spinner = (Spinner) findViewById(R.id.action_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.action_list, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
fillData();
@SuppressWarnings("unused")
View view = (View) getListView().getItemAtPosition(0);
registerForContextMenu(getListView());
if (adapter.isEmpty())
System.out.println("IT'S EMPTY");
String[] projection = { EntryTable.COLUMN_CUSTOMID };
long info = adapter.getItemId(0);
Uri uri = Uri.parse(MyEntryContentProvider.CONTENT_URI + "/"
+ info);
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if(cursor != null)
{
cursor.moveToFirst();
System.out.println((cursor.getString(cursor
.getColumnIndexOrThrow(EntryTable.COLUMN_CUSTOMID))));
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DELETE_ID:
String[] projection = { EntryTable.COLUMN_PHOTOS };
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
Uri uri = Uri.parse(MyEntryContentProvider.CONTENT_URI + "/"
+ info.id);
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
cursor.moveToFirst();
File temp = new File(cursor.getString(cursor
.getColumnIndexOrThrow(EntryTable.COLUMN_PHOTOS)));
if(temp.exists()){
File[] files = temp.listFiles();
for(int i = 0; i < files.length; i++){
files[i].delete();
}
temp.delete();
}
getContentResolver().delete(uri, null, null);
fillData();
return true;
}
return super.onContextItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_home_screen, menu);
//SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_settings:
settCall();
return true;
case R.id.menu_map:
map1_3();
return true;
case R.id.menu_edit:
entryScreen("New Entry");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void entryScreen(String t){
Intent intent = new Intent(this, EntryScreen.class);
intent.putExtra("passer", t);
startActivity(intent);
}
public void settCall(){
Intent sett = new Intent(this,SettingsActivity.class);
startActivity(sett);
}
public void map1_3(){
Intent map13 = new Intent(this,Entrymap_1_3.class);
startActivity(map13);
}
// Opens the second activity if an entry is clicked
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, EntryScreen.class);
Uri itemUri = Uri.parse(MyEntryContentProvider.CONTENT_URI + "/" + id);
i.putExtra(MyEntryContentProvider.CONTENT_ITEM_TYPE, itemUri);
startActivity(i);
}
//@Override
//When we create the loader we're going to get the projection for the ID, customID, species name, and time columns
//(insures that these exist within the database)
//then we create our cursor loader which will be responsible for loading data from the database
//using the projection and our content provider
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
String[] projection = { EntryTable.COLUMN_ID, EntryTable.COLUMN_CUSTOMID, EntryTable.COLUMN_SPECIES, EntryTable.COLUMN_TIME, EntryTable.COLUMN_PHOTOS, EntryTable.COLUMN_PHOTO };
CursorLoader cursorLoader = new CursorLoader(this,
MyEntryContentProvider.CONTENT_URI, projection, null, null, null);
return cursorLoader;
}
private void fillData() {
// Fields from the database (projection)
String[] from = new String[] { EntryTable.COLUMN_CUSTOMID, EntryTable.COLUMN_SPECIES , EntryTable.COLUMN_TIME, EntryTable.COLUMN_PHOTO};
// Fields on the UI to which we map
int[] to = new int[] { R.id.customidlabel, R.id.namelabel, R.id.timelabel, R.id.imageView };
getLoaderManager().initLoader(1, null, this); //changed to 1
adapter = new SimpleCursorAdapter(this, R.layout.list_row, null, from, to, 0);
setListAdapter(adapter);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor data) {
adapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
adapter.swapCursor(null);
}
}
基本上我的应用程序遇到了类似错误而失败的问题。我解决了这个问题,但是一旦我开始将数据添加到数据库中,它就会再次失败。
我希望应用程序在数据库中没有数据可以启动。但是,一旦我添加数据,这个错误就开始了,我无法阻止它。我通过在输入屏幕中输入数据来添加数据。
任何建议都将不胜感激。我也可以添加输入屏幕 - 但是对于Stack Overflow问题,它的时间太长了。
答案 0 :(得分:0)
不知道MyEntryContentProvider的来源,我猜你的数据库的某一行对于列COLUMN_CUSTOMID有一个空值。异常打印输出的“引发者”部分表明该应用程序正在尝试读取onCreate()中的空值列,位于以下行: 的System.out.println((cursor.getString(光标 .getColumnIndexOrThrow(EntryTable.COLUMN_CUSTOMID))));
在onCreate()中,在cursor.moveToFirst()之后,你可以在你的cursor.getString()调用周围添加一个“if(cursor.getColumnCount()&gt; 0){”。