Android 布局问题:无法使用 addView 添加两个图表的两个布局(包括 logcat 错误)
Android Layout issue: Unable to add two layouts of two charts using addView (logcat error included)
我正在尝试使用 AChartEngine 在垂直方向添加两个动态折线图。但是我在布局上遇到了一些问题。下面添加了相关的 java 文件、xml 文件和 logcat(我还有其他 java 文件,例如 Ubidots.java
、Point.java
和 Context.java
但我认为这里不需要这些)。
LineGraph.java:
public class LineGraph {
private GraphicalView view;
private TimeSeries dataset = new TimeSeries("LDR Values");
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
private XYSeriesRenderer renderer = new XYSeriesRenderer(); // This will be used to customize line 1
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); // Holds a collection of XYSeriesRenderer and customizes the graph
private GraphicalView view1;
private TimeSeries dataset1 = new TimeSeries("LDR Values");
private XYSeriesRenderer renderer1 = new XYSeriesRenderer(); // This will be used to customize line 1
public LineGraph()
{
// Add single dataset to multiple dataset
mDataset.addSeries(dataset);
// Customization time for line 1!
renderer.setColor(Color.BLUE);
renderer.setPointStyle(PointStyle.SQUARE);
renderer.setFillPoints(true);
renderer.setDisplayChartValues(true);
renderer.setChartValuesSpacing(10);
// mRenderer: renderer that controls the full charts and add the single renderer for each series:
// Enable Zoom
//mRenderer.setZoomButtonsVisible(true);
mRenderer.setMarginsColor(Color.argb(0x00, 0xff, 0x00, 0x00));
mRenderer.setXTitle("Day #");
mRenderer.setYTitle("LDR Values");
mRenderer.setYAxisMax(35000);
mRenderer.setYAxisMin(20000);
mRenderer.setShowGrid(true);
mRenderer.setInScroll(true);
// Add single renderer to multiple renderer
mRenderer.addSeriesRenderer(renderer);
/////////////////////////////////////
mDataset.addSeries(dataset1);
// Customization time for line 1!
renderer1.setColor(Color.BLUE);
renderer1.setPointStyle(PointStyle.SQUARE);
renderer1.setFillPoints(true);
renderer1.setDisplayChartValues(true);
renderer1.setChartValuesSpacing(10);
// mRenderer: renderer that controls the full charts and add the single renderer for each series:
// Enable Zoom
//mRenderer.setZoomButtonsVisible(true);
mRenderer.setMarginsColor(Color.argb(0x00, 0xff, 0x00, 0x00));
mRenderer.setXTitle("Day #");
mRenderer.setYTitle("Soil Humidity Values");
mRenderer.setYAxisMax(35000);
mRenderer.setYAxisMin(20000);
mRenderer.setShowGrid(true);
mRenderer.setInScroll(true);
// Add single renderer to multiple renderer
mRenderer.addSeriesRenderer(renderer1);
}
public GraphicalView getView(Context context)
{
view = ChartFactory.getLineChartView(context, mDataset, mRenderer);
return view;
}
public void addNewPoints(Point p)
{
dataset.add(p.getX(), p.getY());
}
public GraphicalView getView1(Context context)
{
view1 = ChartFactory.getLineChartView(context, mDataset, mRenderer);
return view1;
}
public void addNewPoints1(Point p1)
{
dataset1.add(p1.getX(), p1.getY());
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.teerna.smartagriculturev5.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/linearParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/chart1"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:background="#a11111"
android:orientation="vertical"/>
<LinearLayout
android:id="@+id/chart2"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:background="#a11111"
android:orientation="vertical"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private static GraphicalView view;
private static GraphicalView view1;
private LineGraph line = new LineGraph();
private LineGraph line1 = new LineGraph();
private static Thread thread;
private static Thread thread1;
LinearLayout row1;
LinearLayout row2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//myWindow = (LinearLayout)findViewById(R.id.paidLayoutLinearParent);
row1 = (LinearLayout) findViewById(R.id.chart1);
row2 = (LinearLayout) findViewById(R.id.chart2);
thread = new Thread() {
public void run()
{
for (int i = 0; i <1000; i++)
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Point p = UbidotsData.getDataFromReceiver(i); // We got new data!
line.addNewPoints(p); // Add it to our graph
view.repaint();
}
}
};
thread.start();
thread1 = new Thread() {
public void run()
{
for (int i = 0; i <1000; i++)
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Point p = UbidotsData.getsoilDataFromReceiver(i); // We got new data!
line1.addNewPoints1(p); // Add it to our graph
view1.repaint();
}
}
};
thread1.start();
}
@Override
protected void onStart() {
super.onStart();
view = line.getView(this);
view = line1.getView1(this);
row1.addView(view,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
row2.addView(view1,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//setContentView(row1);
// setContentView(row2);
}
}
至于最后一部分,当我只使用图表来显示数据时,我使用以下代码来显示实时获取的值的图表:
protected void onStart() {
super.onStart();
view = line.getView(this);
setContentView(view);
}
Logcat:
02-07 21:28:07.203 26865-26865/com.example.teerna.smartagriculturev5 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.teerna.smartagriculturev5/com.example.teerna.smartagriculturev5.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
at android.app.ActivityThread.access0(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.view.ViewGroup.addViewInner(ViewGroup.java:3565)
at android.view.ViewGroup.addView(ViewGroup.java:3437)
at android.view.ViewGroup.addView(ViewGroup.java:3413)
at com.example.teerna.smartagriculturev5.MainActivity.onStart(MainActivity.java:109)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1181)
at android.app.Activity.performStart(Activity.java:5293)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
at android.app.ActivityThread.access0(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
view = line.getView(this);
view = line1.getView1(this);
您将一个字段设置了两次。永远不要成为第二。尝试
view1 = line1.getView1(this);
我正在尝试使用 AChartEngine 在垂直方向添加两个动态折线图。但是我在布局上遇到了一些问题。下面添加了相关的 java 文件、xml 文件和 logcat(我还有其他 java 文件,例如 Ubidots.java
、Point.java
和 Context.java
但我认为这里不需要这些)。
LineGraph.java:
public class LineGraph {
private GraphicalView view;
private TimeSeries dataset = new TimeSeries("LDR Values");
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
private XYSeriesRenderer renderer = new XYSeriesRenderer(); // This will be used to customize line 1
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); // Holds a collection of XYSeriesRenderer and customizes the graph
private GraphicalView view1;
private TimeSeries dataset1 = new TimeSeries("LDR Values");
private XYSeriesRenderer renderer1 = new XYSeriesRenderer(); // This will be used to customize line 1
public LineGraph()
{
// Add single dataset to multiple dataset
mDataset.addSeries(dataset);
// Customization time for line 1!
renderer.setColor(Color.BLUE);
renderer.setPointStyle(PointStyle.SQUARE);
renderer.setFillPoints(true);
renderer.setDisplayChartValues(true);
renderer.setChartValuesSpacing(10);
// mRenderer: renderer that controls the full charts and add the single renderer for each series:
// Enable Zoom
//mRenderer.setZoomButtonsVisible(true);
mRenderer.setMarginsColor(Color.argb(0x00, 0xff, 0x00, 0x00));
mRenderer.setXTitle("Day #");
mRenderer.setYTitle("LDR Values");
mRenderer.setYAxisMax(35000);
mRenderer.setYAxisMin(20000);
mRenderer.setShowGrid(true);
mRenderer.setInScroll(true);
// Add single renderer to multiple renderer
mRenderer.addSeriesRenderer(renderer);
/////////////////////////////////////
mDataset.addSeries(dataset1);
// Customization time for line 1!
renderer1.setColor(Color.BLUE);
renderer1.setPointStyle(PointStyle.SQUARE);
renderer1.setFillPoints(true);
renderer1.setDisplayChartValues(true);
renderer1.setChartValuesSpacing(10);
// mRenderer: renderer that controls the full charts and add the single renderer for each series:
// Enable Zoom
//mRenderer.setZoomButtonsVisible(true);
mRenderer.setMarginsColor(Color.argb(0x00, 0xff, 0x00, 0x00));
mRenderer.setXTitle("Day #");
mRenderer.setYTitle("Soil Humidity Values");
mRenderer.setYAxisMax(35000);
mRenderer.setYAxisMin(20000);
mRenderer.setShowGrid(true);
mRenderer.setInScroll(true);
// Add single renderer to multiple renderer
mRenderer.addSeriesRenderer(renderer1);
}
public GraphicalView getView(Context context)
{
view = ChartFactory.getLineChartView(context, mDataset, mRenderer);
return view;
}
public void addNewPoints(Point p)
{
dataset.add(p.getX(), p.getY());
}
public GraphicalView getView1(Context context)
{
view1 = ChartFactory.getLineChartView(context, mDataset, mRenderer);
return view1;
}
public void addNewPoints1(Point p1)
{
dataset1.add(p1.getX(), p1.getY());
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.teerna.smartagriculturev5.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/linearParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/chart1"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:background="#a11111"
android:orientation="vertical"/>
<LinearLayout
android:id="@+id/chart2"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:background="#a11111"
android:orientation="vertical"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private static GraphicalView view;
private static GraphicalView view1;
private LineGraph line = new LineGraph();
private LineGraph line1 = new LineGraph();
private static Thread thread;
private static Thread thread1;
LinearLayout row1;
LinearLayout row2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//myWindow = (LinearLayout)findViewById(R.id.paidLayoutLinearParent);
row1 = (LinearLayout) findViewById(R.id.chart1);
row2 = (LinearLayout) findViewById(R.id.chart2);
thread = new Thread() {
public void run()
{
for (int i = 0; i <1000; i++)
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Point p = UbidotsData.getDataFromReceiver(i); // We got new data!
line.addNewPoints(p); // Add it to our graph
view.repaint();
}
}
};
thread.start();
thread1 = new Thread() {
public void run()
{
for (int i = 0; i <1000; i++)
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Point p = UbidotsData.getsoilDataFromReceiver(i); // We got new data!
line1.addNewPoints1(p); // Add it to our graph
view1.repaint();
}
}
};
thread1.start();
}
@Override
protected void onStart() {
super.onStart();
view = line.getView(this);
view = line1.getView1(this);
row1.addView(view,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
row2.addView(view1,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
//setContentView(row1);
// setContentView(row2);
}
}
至于最后一部分,当我只使用图表来显示数据时,我使用以下代码来显示实时获取的值的图表:
protected void onStart() {
super.onStart();
view = line.getView(this);
setContentView(view);
}
Logcat:
02-07 21:28:07.203 26865-26865/com.example.teerna.smartagriculturev5 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.teerna.smartagriculturev5/com.example.teerna.smartagriculturev5.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
at android.app.ActivityThread.access0(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.view.ViewGroup.addViewInner(ViewGroup.java:3565)
at android.view.ViewGroup.addView(ViewGroup.java:3437)
at android.view.ViewGroup.addView(ViewGroup.java:3413)
at com.example.teerna.smartagriculturev5.MainActivity.onStart(MainActivity.java:109)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1181)
at android.app.Activity.performStart(Activity.java:5293)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
at android.app.ActivityThread.access0(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
view = line.getView(this);
view = line1.getView1(this);
您将一个字段设置了两次。永远不要成为第二。尝试
view1 = line1.getView1(this);