2014-02-11 45 views
0

我在下面有這門課。爲什麼我在下面的代碼中得到堆棧溢出?

package com.example.metermanager; 

import java.text.DecimalFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Locale; 

import meter.manager.helper.DatabaseHelperClass; 
import meters.model.MeterReading; 
import meters.model.VMeterReadings; 
import meters.model.VReadings; 
import android.content.Context; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

public class EditReadingsAdapter extends BaseAdapter { 
    @SuppressWarnings("unused") 
    private Context context; 
    DatabaseHelperClass db; 
    private static String DB_NAME="meterapp.sqlite";  
    private List<VReadings> readings =new ArrayList<VReadings>(); 
    DecimalFormat df = new DecimalFormat("#,###,###,###"); 
    SimpleDateFormat fm =new SimpleDateFormat("dd-MM-yyyy",Locale.UK); 

    public EditReadingsAdapter(Context context1, String myDate) { 
     this.context=context1; 
     DatabaseHelperClass db= new DatabaseHelperClass(context1,DB_NAME); 
     readings=db.GetMeterReadings(myDate); 
     db.close(); 

    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return readings.size(); 
    } 

    @Override 
    public Object getItem(int index) { 
     // TODO Auto-generated method stub 
     return getItem(index); 
    } 

    @Override 
    public long getItemId(int arg0) { 
     // TODO Auto-generated method stub 
     return arg0; 
    } 

    @Override 
    public View getView(int arg0, View view, ViewGroup parent) { 
     try{   
      if (view ==null){ 
       LayoutInflater inflater= 
         LayoutInflater.from(parent.getContext()); 
         view =inflater.inflate(R.layout.edit_reading_details,parent,false); 

      } 
      VReadings reading =readings.get(arg0);    

      TextView idTextView =(TextView) 
        view.findViewById(R.id.textView6); 

      idTextView.setText(Integer.toString(reading.get_id())); 

      TextView readingdateTextView =(TextView) 
        view.findViewById(R.id.textView7);  
      readingdateTextView.setText(fm.format(((reading.getReadingDate())))); 

      TextView readingTextView =(TextView) 
        view.findViewById(R.id.textView8);  
      readingTextView.setText(df.format((reading.getReading()))); 

      TextView Rate =(TextView) 
         view.findViewById(R.id.textView9);  
      Rate.setText(df.format(reading.getRate())); 

      TextView meterTextView =(TextView) 
         view.findViewById(R.id.textView10);  
       meterTextView.setText(reading.getMeter_number().toString()); 



      }catch(Exception e) 
      { 
       Log.e("Error loading data in listbox",e.toString()); 
      } 
      return view; 
     } 



} 

我使用該類將數據加載到列表視圖窗口小部件中。

public void Search(View v) 
{ 
    try{ 

     Date search=fm.parse(txtviewreadingDate.getText().toString()); 
     String searchDate=fm.format(search); 
     adapter = new EditReadingsAdapter(this ,searchDate); 
     ListView listview =(ListView)findViewById(R.id.lstSearchReadings); 
     listview.setAdapter(adapter); 
     listview.setOnItemClickListener(this); 
     } catch(Exception e) 
     { 
      Log.e("Search Error", e.toString()); 
     } 

} 

我已經在列表視圖上設置了onclickListener事件。

我希望能夠在列表視圖中獲取當前選定的對象,並在自定義對話框中使用它。

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { 
    Context context = this.getApplicationContext();   
    // custom dialog 
    try{ 
       final Dialog dialog = new Dialog(context); 
       dialog.setContentView(R.layout.edit_reading_dialog); 
       dialog.setTitle("Edit Meter Readings"); 

       VReadings r= (VReadings)adapter.getItem(position); 

當我點擊listview時,我得到一個強制關閉錯誤。

這裏是錯誤的Logcat。

02-10 19:15:28.906: I/dalvikvm(5060): threadid=1: stack overflow on call to Lcom/example/metermanager/EditReadingsAdapter;.getItem:LI 
02-10 19:15:28.906: I/dalvikvm(5060): method requires 12+20+8=40 bytes, fp is 0x4428a320 (32 left) 
02-10 19:15:28.916: I/dalvikvm(5060): expanding stack end (0x4428a300 to 0x4428a000) 
02-10 19:15:28.916: I/dalvikvm(5060): Shrank stack (to 0x4428a300, curFrame is 0x4428cebc) 
02-10 19:15:28.916: D/AndroidRuntime(5060): Shutting down VM 
02-10 19:15:28.916: W/dalvikvm(5060): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
02-10 19:15:29.227: E/AndroidRuntime(5060): FATAL EXCEPTION: main 
02-10 19:15:29.227: E/AndroidRuntime(5060): java.lang.StackOverflowError 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.getItem(EditReadingsAdapter.java:47) 
02-10 19:15:29.227: E/AndroidRuntime(5060):  at com.example.metermanager.EditReadingsAdapter.get 
02-10 19:15:29.657: D/dalvikvm(5060): GC_CONCURRENT freed 1491K, 58% free 3062K/7175K, external 4957K/5214K, paused 8ms+8ms 
02-10 19:15:35.306: I/Process(5060): Sending signal. PID: 5060 SIG: 9 

而且它看起來像下面的行是故障警報器。

VReadings r= (VReadings)adapter.getItem(position); 

爲什麼?

羅納德

+0

由於'@覆蓋 公共對象的getItem(INT指數){// TODO自動生成方法存根 返回的getItem(索引); }'也許你wana打電話給超級方法,或者從某個數組中獲取 – Selvin

+0

嗨。我可以通過添加超級? – user3079559

回答

0
@Override 
    public Object getItem(int index) { 
    // TODO Auto-generated method stub 
    return readings.get(index); 
    } 
+0

謝謝。這解決了堆棧問題。我是學習者! – user3079559

2

因爲函數調用getItem本身。你最終會遇到無限的呼叫循環。

public Object getItem(int index) { 
     // TODO Auto-generated method stub 
     return getItem(index); 
} 

由於引擎蓋下的函數參數是通過堆棧傳遞的,所以堆棧溢出結束。