程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> sqlite-android-從光標用數值填滿listview

sqlite-android-從光標用數值填滿listview

編輯:編程綜合問答
android-從光標用數值填滿listview

我寫這些代碼來用在光標處存在的東西來填充listview。但是不起作用,而且應用強制關閉了。我不知道我該怎麼做,請幫幫我。

   public class current_cart extends Activity 
{
DBAdapter db=new DBAdapter(this);
protected void onCreate(Bundle savedInstanceState) {
    _Date=new Date();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.current_cart);
    Lbl_Show=(TextView)this.findViewById(R.id.Lbl_Show_Date);
    this.btn_Remove=(Button)this.findViewById(R.id.Btn_Remove);

    try {

         db.createDataBase();

         } 
     catch (IOException ioe)
         {

         throw new Error("Unable to create database");

         }

         try 
         {

             db.open();

         }catch(SQLException sqle){

         throw sqle;

         }


    Cursor answers = db.f2();
    startManagingCursor(answers);

    ListView list=(ListView)findViewById(R.id.list);    

    String[] from = new String[] {"Cart_ID"};
    int[] to = new int[] {android.R.id.text1};
    @SuppressWarnings("deprecation")
    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, answers, from, to);
    list.setAdapter(cursorAdapter);

    list.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        {
          // 當點擊的時候, 用TextView顯示一個toast
         // setToast("Answer: " + ((TextView) view).getText());
        }
    });
}

}

在DBAdapter.java中這個方法存在:

 public Cursor f2 ()
        {




             Cursor c=null;

c=db.rawQuery("SELECT Cart_ID",null);

             return c;

                  }

這是我的logcat

12-14 09:56:10.608: E/PhonePolicy(32): Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
12-14 09:56:38.747: E/BatteryService(74): usbOnlinePath not found
12-14 09:56:38.747: E/BatteryService(74): batteryVoltagePath not found
12-14 09:56:38.747: E/BatteryService(74): batteryTemperaturePath not found
12-14 09:56:38.837: E/SurfaceFlinger(74): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
12-14 09:56:49.757: E/EventHub(74): could not get driver version for /dev/input/mouse0, Not a typewriter
12-14 09:56:49.757: E/EventHub(74): could not get driver version for /dev/input/mice, Not a typewriter
12-14 09:56:50.246: E/WifiService(74): Invoking mWifiStateMachine.setWifiEnabled
12-14 09:56:53.279: E/ThrottleService(74): Could not open GPS configuration file /etc/gps.conf
12-14 09:56:53.356: E/NetworkTimeUpdateService(74): Could not open GPS configuration file /etc/gps.conf
12-14 09:56:53.377: E/NetworkTimeUpdateService(74): NTP server address not found, not syncing to NTP time
12-14 09:56:53.987: E/logwrapper(145): executing /system/bin/tc failed: No such file or directory
12-14 09:56:54.096: E/logwrapper(146): executing /system/bin/tc failed: No such file or directory
12-14 09:56:54.237: E/logwrapper(147): executing /system/bin/tc failed: No such file or directory
12-14 09:57:07.460: E/SoundPool(74): error loading /system/media/audio/ui/Effect_Tick.ogg
12-14 09:57:07.467: E/SoundPool(74): error loading /system/media/audio/ui/Effect_Tick.ogg
12-14 09:57:07.467: E/SoundPool(74): error loading /system/media/audio/ui/Effect_Tick.ogg
12-14 09:57:07.606: E/SoundPool(74): error loading /system/media/audio/ui/Effect_Tick.ogg
12-14 09:57:07.646: E/SoundPool(74): error loading /system/media/audio/ui/Effect_Tick.ogg
12-14 09:57:07.707: E/SoundPool(74): error loading /system/media/audio/ui/KeypressStandard.ogg
12-14 09:57:07.707: E/SoundPool(74): error loading /system/media/audio/ui/KeypressSpacebar.ogg
12-14 09:57:07.737: E/SoundPool(74): error loading /system/media/audio/ui/KeypressDelete.ogg
12-14 09:57:07.737: E/SoundPool(74): error loading /system/media/audio/ui/KeypressReturn.ogg
12-14 09:57:28.825: E/ActivityManager(74): Load: 6.15 / 1.57 / 0.53
12-14 09:57:28.825: E/ActivityManager(74): CPU usage from 467ms to -11573ms ago:
12-14 09:57:28.825: E/ActivityManager(74):   47% 74/system_server: 31% user + 16% kernel / faults: 5283 minor 2 major
12-14 09:57:28.825: E/ActivityManager(74):   10% 85/bootanimation: 8.8% user + 1.8% kernel
12-14 09:57:28.825: E/ActivityManager(74):   8.5% 200/zygote: 7.3% user + 1.1% kernel / faults: 891 minor
12-14 09:57:28.825: E/ActivityManager(74):   6.7% 124/com.android.systemui: 6.1% user + 0.5% kernel / faults: 519 minor 3 major
12-14 09:57:28.825: E/ActivityManager(74):   5.6% 151/com.android.launcher: 4.8% user + 0.8% kernel / faults: 995 minor 5 major
12-14 09:57:28.825: E/ActivityManager(74):   4.7% 139/zygote: 2.8% user + 1.9% kernel / faults: 425 minor
12-14 09:57:28.825: E/ActivityManager(74):   2.3% 218/app_process: 1.9% user + 0.3% kernel / faults: 269 minor 1 major
12-14 09:57:28.825: E/ActivityManager(74):   1.5% 26/mmcqd: 0% user + 1.5% kernel
12-14 09:57:28.825: E/ActivityManager(74):   1.2% 41/adbd: 0% user + 1.1% kernel
12-14 09:57:28.825: E/ActivityManager(74):   0.2% 32/zygote: 0% user + 0.2% kernel / faults: 32 minor
12-14 09:57:28.825: E/ActivityManager(74):   0.4% 162/android.process.media: 0.3% user + 0% kernel / faults: 30 minor
12-14 09:57:28.825: E/ActivityManager(74):   0.2% 39/rild: 0.1% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):   0.2% 211/fsck_msdos: 0% user + 0.2% kernel / faults: 320 minor
12-14 09:57:28.825: E/ActivityManager(74):   0% 28/servicemanager: 0% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):   0.1% 133/jp.co.omronsoft.openwnn: 0.1% user + 0% kernel / faults: 4 minor
12-14 09:57:28.825: E/ActivityManager(74):   0% 1//init: 0% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):   0% 69/logcat: 0% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):  +0% 228/com.android.defcontainer: 0% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):  +0% 234/com.android.mms: 0% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74): 100% TOTAL: 68% user + 30% kernel + 0.7% irq + 0.1% softirq
12-14 09:57:28.825: E/ActivityManager(74): CPU usage from 7931ms to 10113ms later:
12-14 09:57:28.825: E/ActivityManager(74):   39% 74/system_server: 20% user + 18% kernel / faults: 965 minor
12-14 09:57:28.825: E/ActivityManager(74):     8.3% 88/ActivityManager: 3.2% user + 5.1% kernel
12-14 09:57:28.825: E/ActivityManager(74):     6% 250/Binder Thread #: 0.9% user + 5.1% kernel
12-14 09:57:28.825: E/ActivityManager(74):     5.1% 82/SurfaceFlinger: 3.7% user + 1.3% kernel
12-14 09:57:28.825: E/ActivityManager(74):     4.1% 103/WindowManagerPo: 4.1% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):     1.3% 191/Binder Thread #: 1.3% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):     1.3% 219/Binder Thread #: 0.9% user + 0.4% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.9% 80/Binder Thread #: 0.4% user + 0.4% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.9% 173/Binder Thread #: 0% user + 0.9% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.9% 249/Binder Thread #: 0.9% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.4% 74/system_server: 0% user + 0.4% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.4% 79/Compiler: 0.4% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.4% 81/Binder Thread #: 0% user + 0.4% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.4% 87/er.ServerThread: 0% user + 0.4% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.4% 93/PackageManager: 0.4% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.4% 225/Binder Thread #: 0% user + 0.4% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.4% 251/Binder Thread #: 0.4% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):   11% 85/bootanimation: 8.5% user + 2.6% kernel
12-14 09:57:28.825: E/ActivityManager(74):     9.6% 91/BootAnimation: 7.4% user + 2.1% kernel
12-14 09:57:28.825: E/ActivityManager(74):   12% 200/android.process.acore: 11% user + 0.6% kernel / faults: 40 minor
12-14 09:57:28.825: E/ActivityManager(74):     9.1% 215/Binder Thread #: 7.9% user + 1.2% kernel
12-14 09:57:28.825: E/ActivityManager(74):     1.2% 200/d.process.acore: 1.2% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.6% 206/Compiler: 0.6% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):   10% 124/com.android.systemui: 10% user + 0% kernel / faults: 72 minor
12-14 09:57:28.825: E/ActivityManager(74):     8.8% 124/ndroid.systemui: 8.8% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.5% 130/Compiler: 0% user + 0.5% kernel
12-14 09:57:28.825: E/ActivityManager(74):   10% 151/com.android.launcher: 10% user + 0% kernel / faults: 107 minor 1 major
12-14 09:57:28.825: E/ActivityManager(74):     9.4% 151/ndroid.launcher: 9.4% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):     1.1% 154/HeapWorker: 1.1% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):   9.3% 139/com.android.phone: 7.5% user + 1.7% kernel / faults: 51 minor
12-14 09:57:28.825: E/ActivityManager(74):     8.7% 139/m.android.phone: 6.9% user + 1.7% kernel
12-14 09:57:28.825: E/ActivityManager(74):     4% 184/RILReceiver: 4% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):     1.1% 183/RILSender: 0.5% user + 0.5% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.5% 150/Compiler: 0% user + 0.5% kernel
12-14 09:57:28.825: E/ActivityManager(74):   7% 234/com.android.mms: 3.2% user + 3.8% kernel / faults: 430 minor 6 major
12-14 09:57:28.825: E/ActivityManager(74):     8.9% 234/com.android.mms: 4.4% user + 4.4% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.6% 243/Compiler: 0% user + 0.6% kernel
12-14 09:57:28.825: E/ActivityManager(74):   0.5% 39/rild: 0.3% user + 0.1% kernel
12-14 09:57:28.825: E/ActivityManager(74):     1% 58/rild: 0.7% user + 0.3% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.3% 60/rild: 0.3% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):   0.9% 41/adbd: 0.4% user + 0.4% kernel
12-14 09:57:28.825: E/ActivityManager(74):     0.4% 41/adbd: 0.4% user + 0% kernel
12-14 09:57:28.825: E/ActivityManager(74):   0.6% 228/com.android.defcontainer: 0% user + 0.6% kernel / faults: 30 minor
12-14 09:57:28.825: E/ActivityManager(74):     0.6% 245/Binder Thread #: 0% user + 0.6% kernel
12-14 09:57:28.825: E/ActivityManager(74): 100% TOTAL: 72% user + 27% kernel
12-14 09:57:29.859: E/InputDispatcher(74): channel '409e7970 NotificationPanel (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
12-14 09:57:29.865: E/InputDispatcher(74): channel '409e7970 NotificationPanel (server)' ~ Channel is unrecoverably broken and will be disposed!
12-14 09:57:30.035: E/InputDispatcher(74): Received spurious receive callback for unknown input channel.  fd=170, events=0x8
12-14 09:57:30.035: E/InputDispatcher(74): Received spurious receive callback for unknown input channel.  fd=174, events=0x8
12-14 09:57:30.035: E/InputDispatcher(74): Received spurious receive callback for unknown input channel.  fd=188, events=0x8
12-14 09:57:38.786: E/TelephonyManager(74): Hidden constructor called more than once per process!
12-14 09:57:38.786: E/TelephonyManager(74): Original: android, new: android
12-14 09:57:39.474: E/ActivityManager(74): Start proc com.android.email for broadcast com.android.email/.service.EmailBroadcastReceiver: pid=302 uid=10013 gids={3003, 1015}Load: 8.44 / 2.21 / 0.75
12-14 09:57:39.474: E/ActivityManager(74): CPU usage from 2651ms to -8566ms ago:
12-14 09:57:39.474: E/ActivityManager(74):   41% 74/system_server: 28% user + 13% kernel / faults: 2614 minor
12-14 09:57:39.474: E/ActivityManager(74):   13% 151/com.android.launcher: 12% user + 1.6% kernel / faults: 772 minor 4 major
12-14 09:57:39.474: E/ActivityManager(74):   10% 139/zygote: 9.2% user + 1.5% kernel / faults: 542 minor
12-14 09:57:39.474: E/ActivityManager(74):   10% 85/bootanimation: 9.1% user + 1.2% kernel
12-14 09:57:39.474: E/ActivityManager(74):   4.6% 32/zygote: 3.5% user + 1% kernel / faults: 258 minor
12-14 09:57:39.474: E/ActivityManager(74):   2.5% 234/com.android.mms: 1.6% user + 0.9% kernel / faults: 561 minor 2 major
12-14 09:57:39.474: E/ActivityManager(74):   1.7% 200/zygote: 1.4% user + 0.3% kernel / faults: 94 minor
12-14 09:57:39.474: E/ActivityManager(74):   1.6% 41/adbd: 0.1% user + 1.4% kernel / faults: 1 minor
12-14 09:57:39.474: E/ActivityManager(74):   0.3% 69/logcat: 0% user + 0.2% kernel
12-14 09:57:39.474: E/ActivityManager(74):   0.1% 211/fsck_msdos: 0.1% user + 0% kernel / faults: 106 minor
12-14 09:57:39.474: E/ActivityManager(74):   0% 1//init: 0% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74):   0% 4/events/0: 0% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74):  +0% 272/com.android.systemui: 0% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74):  +0% 287/com.android.deskclock: 0% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74):  +0% 302/zygote: 0% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74): 100% TOTAL: 73% user + 26% kernel + 0.5% irq + 0.2% softirq
12-14 09:57:39.474: E/ActivityManager(74): CPU usage from 7161ms to 8260ms later:
12-14 09:57:39.474: E/ActivityManager(74):   62% 74/system_server: 49% user + 13% kernel / faults: 94 minor
12-14 09:57:39.474: E/ActivityManager(74):     17% 102/WindowManager: 17% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74):     16% 88/ActivityManager: 8.4% user + 7.5% kernel
12-14 09:57:39.474: E/ActivityManager(74):     5.6% 82/SurfaceFlinger: 4.7% user + 0.9% kernel
12-14 09:57:39.474: E/ActivityManager(74):     4.7% 107/ConnectivitySer: 1.8% user + 2.8% kernel
12-14 09:57:39.474: E/ActivityManager(74):     3.7% 75/HeapWorker: 1.8% user + 1.8% kernel
12-14 09:57:39.474: E/ActivityManager(74):     3.7% 103/WindowManagerPo: 3.7% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74):     1.8% 79/Compiler: 0.9% user + 0.9% kernel
12-14 09:57:39.474: E/ActivityManager(74):     1.8% 251/Binder Thread #: 1.8% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74):     0.9% 80/Binder Thread #: 0.9% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74):     0.9% 173/Binder Thread #: 0.9% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74):     0.9% 191/Binder Thread #: 0.9% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74):     0.9% 250/Binder Thread #: 0% user + 0.9% kernel
12-14 09:57:39.474: E/ActivityManager(74):   12% 287/com.android.deskclock: 7.8% user + 4.4% kernel / faults: 213 minor
12-14 09:57:39.474: E/ActivityManager(74):     11% 299/AsyncHandler: 7.8% user + 3.3% kernel
12-14 09:57:39.474: E/ActivityManager(74):   8.3% 85/bootanimation: 8.3% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74):     13% 91/BootAnimation: 13% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74):   4.1% 139/com.android.phone: 3.1% user + 1% kernel / faults: 6 minor
12-14 09:57:39.474: E/ActivityManager(74):     3.1% 139/m.android.phone: 3.1% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74):     1% 264/Binder Thread #: 1% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74):   2.8% 41/adbd: 0.9% user + 1.8% kernel
12-14 09:57:39.474: E/ActivityManager(74):     0.9% 41/adbd: 0.9% user + 0% kernel
12-14 09:57:39.474: E/ActivityManager(74):     0.9% 65/adbd: 0% user + 0.9% kernel
12-14 09:57:39.474: E/ActivityManager(74):   1.9% 32/zygote: 0% user + 1.9% kernel / faults: 7 minor
12-14 09:57:39.474: E/ActivityManager(74): 100% TOTAL: 80% user + 19% kernel

最佳回答:


你應該將你的LogCat貼出來,但是現在我發現了一個問題

public class CurrentCart extends Activity 
{
    DBAdapter db;
    protected void onCreate(Bundle savedInstanceState) {
        db=new DBAdapter(this);
        ...

在onCreate()之前,你不能在一個activity中引用這個(或者是其他子類的context)

#注意
LogCat並不是直接指向你的app,將你捕獲異常的改成:

try {
    db.createDataBase();
    db.open();
} 
catch (Exception e) {
    e.printStackTrace();
}

然後看看你的LogCat裡有沒有什麼特殊的情況出現。

zhengdanimu
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved