-2

プロジェクトにエラーはないようですが、アクティビティにアクセスすると強制終了し、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
4

1 に答える 1

0

文字列配列を初期化していません。したがって、onCreate() で以下のように初期化してください。

 private String[] listview_array;
     /** Called when the activity is first created. */
     @Override
    public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.search);
            listview_array=new String[10];

編集: 以下のように文字列配列にデータを追加してから、文字列配列をアダプターに追加します。

 listview_array= mySQLiteAdapter.qall();
   listContent1.setAdapter(new ArrayAdapter<String>(this,
      android.R.layout.simple_list_item_1, listview_array));
于 2013-03-06T06:38:48.540 に答える