有人可以帮助我.. 我有一个问题,日志显示如下 LOG
03-22 16:22:04.804: E/AndroidRuntime(5329): FATAL EXCEPTION: main
03-22 16:22:04.804: E/AndroidRuntime(5329): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thesis.awsapp/com.thesis.awsapp.Graph}: java.lang.NullPointerException
03-22 16:22:04.804: E/AndroidRuntime(5329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-22 16:22:04.804: E/AndroidRuntime(5329): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
03-22 16:22:04.804: E/AndroidRuntime(5329): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-22 16:22:04.804: E/AndroidRuntime(5329): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
03-22 16:22:04.804: E/AndroidRuntime(5329): at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 16:22:04.804: E/AndroidRuntime(5329): at android.os.Looper.loop(Looper.java:130)
03-22 16:22:04.804: E/AndroidRuntime(5329): at android.app.ActivityThread.main(ActivityThread.java:3687)
03-22 16:22:04.804: E/AndroidRuntime(5329): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 16:22:04.804: E/AndroidRuntime(5329): at java.lang.reflect.Method.invoke(Method.java:507)
03-22 16:22:04.804: E/AndroidRuntime(5329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
03-22 16:22:04.804: E/AndroidRuntime(5329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
03-22 16:22:04.804: E/AndroidRuntime(5329): at dalvik.system.NativeStart.main(Native Method)
03-22 16:22:04.804: E/AndroidRuntime(5329): Caused by: java.lang.NullPointerException
03-22 16:22:04.804: E/AndroidRuntime(5329): at com.thesis.awsapp.Graph.OpenChart(Graph.java:186)
03-22 16:22:04.804: E/AndroidRuntime(5329): at com.thesis.awsapp.Graph.onCreate(Graph.java:54)
03-22 16:22:04.804: E/AndroidRuntime(5329): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-22 16:22:04.804: E/AndroidRuntime(5329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
03-22 16:22:04.804: E/AndroidRuntime(5329): ... 11 more
03-22 16:22:04.820: E/liblog(1486): failed to call dumpstate
03-22 16:22:04.828: E/(1486): Dumpstate > /data/log/dumpstate_app_error
这是我使用的代码
CODE:
package com.thesis.awsapp;
import java.util.ArrayList;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.SeriesSelection;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Graph extends Activity {
private GraphicalView mChart;
String[] mMonth;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.graph_layout);
Intent intent = getIntent();
String Query = intent.getExtras().getString("Query");
getDataa mDoPOST = new getDataa(Graph.this, Query);
mDoPOST.execute("");
OpenChart();
}
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
public class getDataa extends AsyncTask<String, Void, ArrayList<String>> {
String strNameToSearch = "";
ArrayList<Double> list = new ArrayList<Double>();
ArrayList<String> Date = new ArrayList<String>();
// Result data
Exception exception = null;
getDataa(Context context, String nameToSearch) {
strNameToSearch = nameToSearch;
}
@Override
protected ArrayList<String> doInBackground(String... arg0) {
try {
// Setup the parameters
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("QUERY",
strNameToSearch));
// Add more parameters as necessary
// Create the HTTP request
HttpParams httpParameters = new BasicHttpParams();
// Setup timeouts
HttpConnectionParams.setConnectionTimeout(httpParameters,
15000);
HttpConnectionParams.setSoTimeout(httpParameters, 15000);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(
"http://210.14.5.179/aws/getLocal2.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
// Create a JSON object from the request response
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.getJSONArray("tblStations");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject curr = jsonArray.getJSONObject(i);
list.add(Double.parseDouble(curr.getString("temperature")));
Date.add(curr.getString("Time"));
}
} catch (Exception e) {
Log.e("ClientServerDemo", "Error:", e);
exception = e;
}
return Date;
}
@Override
protected void onPostExecute(ArrayList<String> Date) {
mMonth =Date.toArray(new String[Date.size()]);
}
}
// /////////////////////////////////////
private void OpenChart() {
// Define the number of elements you want in the chart.
int z[] = { 0, 1, 2, 3, 4, 5, 6, 7 };
int x[] = { 10, 18, 32, 21, 48, 60, 53, 80 };
// Create XY Series for X Series.
XYSeries xSeries = new XYSeries("X Series");
// Adding data to the X Series.
for (int i = 0; i < z.length; i++) {
xSeries.add(z[i], x[i]);
}
// Create a Dataset to hold the XSeries.
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Add X series to the Dataset.
dataset.addSeries(xSeries);
// Create XYSeriesRenderer to customize XSeries
XYSeriesRenderer Xrenderer = new XYSeriesRenderer();
Xrenderer.setColor(Color.GREEN);
Xrenderer.setPointStyle(PointStyle.DIAMOND);
Xrenderer.setDisplayChartValues(true);
Xrenderer.setLineWidth(2);
Xrenderer.setFillPoints(true);
// Create XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
mRenderer.setChartTitle("X Vs Y Chart");
mRenderer.setXTitle("X Values");
mRenderer.setYTitle("Y Values");
mRenderer.setZoomButtonsVisible(true);
mRenderer.setXLabels(0);
mRenderer.setPanEnabled(false);
mRenderer.setShowGrid(true);
mRenderer.setClickEnabled(true);
for (int i = 0; i < z.length; i++) {
mRenderer.addXTextLabel(i, mMonth[i]);
}
// Adding the XSeriesRenderer to the MultipleRenderer.
mRenderer.addSeriesRenderer(Xrenderer);
LinearLayout chart_container = (LinearLayout) findViewById(R.id.Chart_layout);
// Creating an intent to plot line chart using dataset and
// multipleRenderer
mChart = (GraphicalView) ChartFactory.getLineChartView(
getBaseContext(), dataset, mRenderer);
// Adding click event to the Line Chart.
mChart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SeriesSelection series_selection = mChart
.getCurrentSeriesAndPoint();
if (series_selection != null) {
int series_index = series_selection.getSeriesIndex();
String select_series = "X Series";
if (series_index == 0) {
select_series = "X Series";
} else {
select_series = "Y Series";
}
String month = mMonth[(int) series_selection.getXValue()];
int amount = (int) series_selection.getValue();
Toast.makeText(getBaseContext(),
select_series + "in" + month + ":" + amount,
Toast.LENGTH_LONG).show();
}
}
});
// Add the graphical view mChart object into the Linear layout .
chart_container.addView(mChart);
}
}
有人可以推荐一些东西吗?我试图从网络服务器中绘制数据。 在这段代码中,我想要得到时间并将其作为X轴标签。
还有人可以给我一些关于如何从网络服务器绘制数据图表的提示吗?
非常感谢
这是我的XML文件
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/Chart_layout"
android:orientation="vertical">
</LinearLayout>
答案 0 :(得分:0)
您正在访问mMonth
中的OpenChart()
数组,但您只能在稍后发生的异步任务onPostExecute()
中对其进行初始化。
使用合理的默认值初始化mMonth
,跳过您需要的部分mMonth
或完全推迟OpenChart()
,直到数据可用。