我的项目似乎没有错误,但是当我访问我的活动时,它会强制关闭,这会出现在我的 logcat 中。我的代码有什么问题?
03-06 14:03:36.994: E/AndroidRuntime(20120): FATAL EXCEPTION: main
03-06 14:03:36.994: E/AndroidRuntime(20120): java.lang.RuntimeException: Unable to start activity ComponentInfo{me.mojica.caloriewatch/me.mojica.caloriewatch.Search}: java.lang.NullPointerException
03-06 14:03:36.994: E/AndroidRuntime(20120): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-06 14:03:36.994: E/AndroidRuntime(20120): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-06 14:03:36.994: E/AndroidRuntime(20120): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-06 14:03:36.994: E/AndroidRuntime(20120): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-06 14:03:36.994: E/AndroidRuntime(20120): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 14:03:36.994: E/AndroidRuntime(20120): at android.os.Looper.loop(Looper.java:123)
03-06 14:03:36.994: E/AndroidRuntime(20120): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-06 14:03:36.994: E/AndroidRuntime(20120): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 14:03:36.994: E/AndroidRuntime(20120): at java.lang.reflect.Method.invoke(Method.java:521)
03-06 14:03:36.994: E/AndroidRuntime(20120): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-06 14:03:36.994: E/AndroidRuntime(20120): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-06 14:03:36.994: E/AndroidRuntime(20120): at dalvik.system.NativeStart.main(Native Method)
03-06 14:03:36.994: E/AndroidRuntime(20120): Caused by: java.lang.NullPointerException
03-06 14:03:36.994: E/AndroidRuntime(20120): at java.util.Arrays$ArrayList.<init>(Arrays.java:49)
03-06 14:03:36.994: E/AndroidRuntime(20120): at java.util.Arrays.asList(Arrays.java:171)
03-06 14:03:36.994: E/AndroidRuntime(20120): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:125)
03-06 14:03:36.994: E/AndroidRuntime(20120): at me.mojica.caloriewatch.Search.onCreate(Search.java:117)
03-06 14:03:36.994: E/AndroidRuntime(20120): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-06 14:03:36.994: E/AndroidRuntime(20120): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-06 14:03:36.994: E/AndroidRuntime(20120): ... 11 more
这是我的活动,当我尝试打开它时,会立即出现强制关闭窗口
private String[] listview_array;
private ArrayList<String> array_sort= new ArrayList<String>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
listContent1 = (ListView)findViewById(R.id.lFoodlist1);
search= (EditText) findViewById(R.id.eFsearch);
food= (EditText) findViewById(R.id.editText1);
calories= (EditText) findViewById(R.id.editText2);
FSearch=(Button) findViewById(R.id.bFsearch);
result=(TextView)findViewById(R.id.textView3);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[]{SQLiteAdapter.KEY_FOODNAME,SQLiteAdapter.KEY_CALORIES};
int[] to = new int[]{R.id.tv1, R.id.tv2};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
//listContent1.setAdapter(cursorAdapter);
listContent1.setOnItemClickListener(listContentOnItemClickListener);
mySQLiteAdapter.close();
listContent1.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listview_array));
search.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textlength = search.getText().length();
array_sort.clear();
for (int i = 0; i < listview_array.length; i++)
{
if (textlength <= listview_array[i].length())
{
if(search.getText().toString().equalsIgnoreCase(
(String)
listview_array[i].subSequence(0,
textlength)))
{
array_sort.add(listview_array[i]);
}
}
}
listContent1.setAdapter(new ArrayAdapter<String>
(Search.this,
android.R.layout.simple_list_item_1, array_sort));
}
});
}
}
适配器
public String[] qall(){
Cursor cursor = null;
String[] columns = new String[]{KEY_ID, KEY_FOODNAME, KEY_CALORIES};
String[] listview_array = null;
try{
cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null, null, null, null, null);
if(cursor!=null && cursor.getCount()>0 && cursor.moveToFirst()){
listview_array=new String[cursor.getCount()];
int foodName = cursor.getColumnIndex(KEY_FOODNAME );
int keyColories = cursor.getColumnIndex(KEY_CALORIES );
int l=0;
boolean moveToNext = cursor.moveToFirst();
do{
listview_array[l]=cursor.getString(foodName) + " " +
cursor.getString(keyColories);
l++;
}while(cursor.moveToNext());
}
}catch(Exception e){
Log.e(APP_NAME, "An error occurred while searching for "+search+": "+e.toString(), e);
}finally{
if(cursor!=null && !cursor.isClosed()){
cursor.close();
}
}
return listview_array;
}
我的整个代码。请帮忙
最新错误
03-06 15:07:08.316: E/AndroidRuntime(23106): FATAL EXCEPTION: main
03-06 15:07:08.316: E/AndroidRuntime(23106): java.lang.RuntimeException: Unable to start activity ComponentInfo{me.mojica.caloriewatch/me.mojica.caloriewatch.Search}: java.lang.NullPointerException
03-06 15:07:08.316: E/AndroidRuntime(23106): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-06 15:07:08.316: E/AndroidRuntime(23106): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-06 15:07:08.316: E/AndroidRuntime(23106): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-06 15:07:08.316: E/AndroidRuntime(23106): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-06 15:07:08.316: E/AndroidRuntime(23106): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 15:07:08.316: E/AndroidRuntime(23106): at android.os.Looper.loop(Looper.java:123)
03-06 15:07:08.316: E/AndroidRuntime(23106): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-06 15:07:08.316: E/AndroidRuntime(23106): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 15:07:08.316: E/AndroidRuntime(23106): at java.lang.reflect.Method.invoke(Method.java:521)
03-06 15:07:08.316: E/AndroidRuntime(23106): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-06 15:07:08.316: E/AndroidRuntime(23106): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-06 15:07:08.316: E/AndroidRuntime(23106): at dalvik.system.NativeStart.main(Native Method)
03-06 15:07:08.316: E/AndroidRuntime(23106): Caused by: java.lang.NullPointerException
03-06 15:07:08.316: E/AndroidRuntime(23106): at java.util.Arrays$ArrayList.<init>(Arrays.java:49)
03-06 15:07:08.316: E/AndroidRuntime(23106): at java.util.Arrays.asList(Arrays.java:171)
03-06 15:07:08.316: E/AndroidRuntime(23106): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:125)
03-06 15:07:08.316: E/AndroidRuntime(23106): at me.mojica.caloriewatch.Search.onCreate(Search.java:117)
03-06 15:07:08.316: E/AndroidRuntime(23106): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-06 15:07:08.316: E/AndroidRuntime(23106): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-06 15:07:08.316: E/AndroidRuntime(23106): ... 11 more