2012-10-25 67 views
0

我的應用程序的logcat顯示一些IndexOutOfBound異常。在這裏我試圖創建一個條形圖在android中使用achartengine庫。並且我得到x軸而從消費它的Web服務y軸值,我下面的消耗網絡service.Can任何一個讓我知道我犯我對我的項目是什麼錯誤SOAP的方法?Logcat顯示我的Android應用程序運行後IndexOutOfBound異常

建議請!..

感謝您的寶貴時間!..

logcat的

10-25 11:44:32.713: W/KeyCharacterMap(928): No keyboard for id 0 
10-25 11:44:32.723: W/KeyCharacterMap(928): Using default keymap: /system/usr/keychars/qwerty.kcm.bin 
10-25 11:44:40.383: D/AndroidRuntime(928): Shutting down VM 
10-25 11:44:40.383: W/dalvikvm(928): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
10-25 11:44:40.393: E/AndroidRuntime(928): FATAL EXCEPTION: main 
10-25 11:44:40.393: E/AndroidRuntime(928): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 
10-25 11:44:40.393: E/AndroidRuntime(928): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257) 
10-25 11:44:40.393: E/AndroidRuntime(928): at java.util.ArrayList.get(ArrayList.java:311) 
10-25 11:44:40.393: E/AndroidRuntime(928): at com.example.barchart_test.Chart_MainActivity$1.onClick(Chart_MainActivity.java:43) 
10-25 11:44:40.393: E/AndroidRuntime(928): at android.view.View.performClick(View.java:2408) 
10-25 11:44:40.393: E/AndroidRuntime(928): at android.view.View$PerformClick.run(View.java:8816) 
10-25 11:44:40.393: E/AndroidRuntime(928): at android.os.Handler.handleCallback(Handler.java:587) 
10-25 11:44:40.393: E/AndroidRuntime(928): at android.os.Handler.dispatchMessage(Handler.java:92) 
10-25 11:44:40.393: E/AndroidRuntime(928): at android.os.Looper.loop(Looper.java:123) 
10-25 11:44:40.393: E/AndroidRuntime(928): at android.app.ActivityThread.main(ActivityThread.java:4627) 
10-25 11:44:40.393: E/AndroidRuntime(928): at java.lang.reflect.Method.invokeNative(Native Method) 
10-25 11:44:40.393: E/AndroidRuntime(928): at java.lang.reflect.Method.invoke(Method.java:521) 
10-25 11:44:40.393: E/AndroidRuntime(928): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
10-25 11:44:40.393: E/AndroidRuntime(928): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
10-25 11:44:40.393: E/AndroidRuntime(928): at dalvik.system.NativeStart.main(Native Method) 

請找我的消息來源參考

Chart_MainActivity

public class Chart_MainActivity extends Activity 
{ 
EditText edt1, edt2; 
// TextView txt_1; 
Button btn; 
ArrayList<String> result; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

edt1 = (EditText) findViewById(R.id.edt1_date1); 
edt2 = (EditText) findViewById(R.id.edt_date2); 
btn = (Button) findViewById(R.id.button_invoke); 

result = new ArrayList<String>(); 

btn.setOnClickListener(new View.OnClickListener() 
{ 
    public void onClick(View v) 
    { 
     result.addAll(getTMSChart(edt1.getText().toString(), edt2.getText().toString())); 

     Intent in = new Intent(getApplicationContext(), NextActivity.class); 
     in.putExtra("gotonextpageX", result.get(0)); 
     in.putExtra("gotonextpageY", result.get(1)); 

     startActivity(in); 
    } 
});} 

private ArrayList<String> getTMSChart(String FromDate, String ToDate) 
{ 

// txt_1 = (TextView)findViewById(R.id.textView1); 
System.setProperty("http.keepAlive", "false"); 
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

envelope.dotNet = true; 

String NAMESPACE = "http://tempuri.org/"; 
String URL = "http://54.251.60.177/TMSOrdersService/TMSDetails.asmx"; 
String METHOD = "GetTMSChart"; 

SoapObject request = new SoapObject(NAMESPACE, METHOD); 
request.addProperty("FromDate", FromDate); 
request.addProperty("ToDate", ToDate); 

envelope.setOutputSoapObject(request); 
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

String x = "", y = ""; 

ArrayList<String> stringResult = new ArrayList<String>(); 

try 
{ 
    androidHttpTransport.call(NAMESPACE + METHOD, envelope); 
    SoapObject result = (SoapObject) envelope.bodyIn; 
    SoapObject root = (SoapObject) ((SoapObject) (result).getProperty(0)).getProperty("NewDataSet"); 
    int tablesCount = root.getPropertyCount(); 

    for (int i = 0; i < tablesCount; i++) 
    { 
     SoapObject table = (SoapObject) root.getProperty(i); 
     int propertyCount = table.getPropertyCount(); 

     for (int j = 0; j < propertyCount; j++) 
     { 

      stringResult.add(table.getPropertyAsString("Order_No").toString()); 
      stringResult.add(table.getPropertyAsString("Freight_Rate").toString()); 

     // stringResult.add(table.getPropertyAsString("Margin_Percent").toString()); 

     // freightRate = Integer.parseInt(table.getPropertyAsString("Freight_Rate")); 

     } 
    } 
} 
catch (Exception e) 
{} 

return stringResult; 
} 
} 

NextActivity

public class NextActivity extends Activity 
{ 
double x,y; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main1); 

    Bundle extras = getIntent().getExtras(); 
    if(extras != null) 
    { 
     x = extras.getDouble("gotonextpageX"); 
     y = extras.getDouble("gotonextpageY"); 
    } 
    else 
    {} 

    final GraphicalView gv =createIntent(); 
    RelativeLayout rl=(RelativeLayout)findViewById(R.id.rlt); 
    rl.addView(gv); 
} 

public GraphicalView createIntent() 
{ 

    String[] titles = new String[] { "Orders profit"}; 

    List<double[]> values = new ArrayList<double[]>(); 
    values.add(new double[] { x }); 
    values.add(new double[] { y }); 


    int[] colors = new int[] { Color.parseColor("#77c4d3")}; 

    XYMultipleSeriesRenderer renderer = buildBarRenderer(colors); 
    renderer.setOrientation(Orientation.HORIZONTAL); 

    setChartSettings(renderer,"","","",0.5,12.5, 0, y, Color.GREEN, Color.GREEN); 

    renderer.setXLabels(1); 
    renderer.setYLabels(10); 

    renderer.setPanEnabled(true, false); 
    renderer.addXTextLabel(1, "x"); 

    /*renderer.addXTextLabel(2, "Feb"); 
    renderer.addXTextLabel(3, "Mar"); 
    renderer.addXTextLabel(4, "Apr"); 
    renderer.addXTextLabel(5, "May"); 
    renderer.addXTextLabel(6, "Jun");*/ 


    int length = renderer.getSeriesRendererCount(); 

    for (int i = 0; i < length; i++) 
    { 
     SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i); 
     seriesRenderer.setDisplayChartValues(true); 
     } 

    final GraphicalView grfv = ChartFactory.getBarChartView(NextActivity.this, buildBarDataset(titles, values), renderer,Type.DEFAULT); 
    return grfv; 
    } 

    protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) 
    { 

     XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer(); 

     renderer.setAxisTitleTextSize(16); 
     renderer.setChartTitleTextSize(20); 

     renderer.setLabelsTextSize(15); 
     renderer.setLegendTextSize(15); 

     renderer.setBarSpacing(1); 

     renderer.setMarginsColor(Color.parseColor("#0E120E")); 
     renderer.setXLabelsColor(Color.WHITE); 
     renderer.setYLabelsColor(0,Color.WHITE); 

     renderer.setApplyBackgroundColor(true); 
     renderer.setBackgroundColor(Color.parseColor("#000000")); 

     int length = colors.length; 

     for (int i = 0; i < length; i++) 
     { 
      SimpleSeriesRenderer r = new SimpleSeriesRenderer(); 
      r.setColor(colors[i]); 
      r.setChartValuesSpacing(-90); 
      r.setChartValuesSpacing(15); 
      renderer.addSeriesRenderer(r); 
      } 
      return renderer; 
     } 

    protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) 
    { 
     XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); 
     int length = titles.length; 

    for (int i = 0; i < length; i++) 
    { 
     CategorySeries series = new CategorySeries(titles[i]); 
     double[] v = values.get(i); 
     int seriesLength = v.length; 

     for (int k = 0; k < seriesLength; k++) 
     { 
      series.add(v[k]); 
      } 
     dataset.addSeries(series.toXYSeries()); 
     } 

    return dataset; 
    } 

protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle, 
String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,int labelsColor) 
{ 

    renderer.setChartTitle(title); 
    renderer.setYLabelsAlign(Align.RIGHT); 

    renderer.setXTitle(xTitle); 
    renderer.setYTitle(yTitle); 

    renderer.setXAxisMin(xMin); 
    renderer.setXAxisMax(xMax); 
    renderer.setYAxisMin(yMin); 
    renderer.setYAxisMax(yMax); 

    renderer.setMargins(new int[] { 10, 65, 10, 15 }); 

    renderer.setAxesColor(axesColor); 
    renderer.setLabelsColor(labelsColor); 
    } 
} 

的Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.barchart_test" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" android:maxSdkVersion="15"/> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity android:name=".Chart_MainActivity" 
       android:label="@string/title_activity_chart__main" > 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
    </activity> 

    <activity android:name=".NextActivity" 
       android:label="@string/title_activity_chart__main" > 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN1" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
    </activity> 

</application> 
</manifest> 
+0

你能告訴你的Manifest.xml – VenomVendor

+0

檢查這條線'com.example.barchart_test.Chart_MainActivity $ 1.onClick(Chart_MainActivity.java:43)' –

+0

的'克etTMSChart'方法返回一個空列表(可能是try-catch塊中的一個異常),然後嘗試獲取元素。 – Luksprog

回答

1

你確定你正從服務器端的東西嗎?在從中獲取元素之前,您不檢查結果列表大小。 至於我在這個問題上弦樂器 in.putExtra( 「gotonextpageX」,result.get(0)); in.putExtra(「gotonextpageY」,result.get(1));

+0

我相信我收到從服務器成功 – Rohith

+0

咦x軸和y軸的值?無論你在哪裏使用x和y,都不要將它們添加到ArrayList中。返回的列表只是空的。另外**你在主線程**上進行網絡調用。您不應該這樣做 - 如果持續時間過長,您的用戶將看到ANR錯誤消息! –

0

出界異常的添加數組索引到你的主要活動代碼

try 
{ 
    androidHttpTransport.call(NAMESPACE + METHOD, envelope); 
    SoapObject result = (SoapObject) envelope.bodyIn; 
    SoapObject root = (SoapObject) ((SoapObject) (result).getProperty(0)).getProperty("NewDataSet"); 
    int tablesCount = root.getPropertyCount(); 

    for (int i = 0; i < tablesCount; i++) 
    { 
     SoapObject table = (SoapObject) root.getProperty(i); 
     int propertyCount = table.getPropertyCount(); 

     for (int j = 0; j < propertyCount; j++) 
     { 

      stringResult.add(table.getPropertyAsString("Order_No").toString()); 
      stringResult.add(table.getPropertyAsString("Freight_Rate").toString()); 

     // stringResult.add(table.getPropertyAsString("Margin_Percent").toString()); 

     // freightRate = Integer.parseInt(table.getPropertyAsString("Freight_Rate")); 

     } 
    } 
} 
catch (ArrayIndexOutOfBoundException e) 
{} 
catch (Exception e) 
{} 
+0

至於我,捕捉運行時異常 - 是不好的做法。 – Viacheslav

-1

嘗試通過

更換

<activity android:name=".NextActivity" 
      android:label="@string/title_activity_chart__main" > 
<intent-filter> 
    <action android:name="android.intent.action.MAIN1" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
</intent-filter> 
</activity> 

<activity android:name=".NextActivity"></activity> 
+0

這不會是嚴重的問題,我認爲是這樣 – Rohith

相關問題