0

有没有人在您的 logcat 上遇到以下问题?在这里,我试图在 achartengine 库的帮助下在 android 中创建一个条形图。但问题是我通过使用它从 Web 服务获取 x 和 y 轴值。

建议请..

提前致谢!..

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

Chart_MainActivity.java

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()));

    //  result.add(x);
    //  result.add(y);
        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 (ArrayIndexOutOfBoundsException e) 
{}
catch(Exception e)
{}

return stringResult;
}    }

NextActivity.java

 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,x,x});
    values.add(new double[] {y,y,y});


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

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

    setChartSettings(renderer,"","Order number","Freight Rate",0.5,12.5, 0, 1000, Color.GREEN, Color.GREEN);

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

    renderer.setPanEnabled(true, false);

    /*renderer.addXTextLabel(1, "Jan");
    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);
  } }

主要的.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<AbsoluteLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/edt1_date1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="46dp"
        android:layout_y="116dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/edt_date2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="46dp"
        android:layout_y="180dp"
        android:ems="10" />

    <Button
        android:id="@+id/button_invoke"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="204dp"
        android:layout_y="246dp"
        android:text="Invoke" />
</AbsoluteLayout>
</LinearLayout>

main1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

</RelativeLayout>

清单.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"></activity>
    <activity android:name="org.achartengine.GraphicalActivity" />

</application>
</manifest>
4

1 回答 1

0

尽管该问题并未描述所有方面,但这里看起来像是一个简单的问题。

我们可以很容易地猜到主活动类中的第 46 行是下面这一行:

in.putExtra("gotonextpageX", result.get(0));

result列表中没有任何元素,您正在尝试获取第一个元素,因此IndexOutOfBounds引发了异常。

于 2012-10-27T14:12:27.633 回答