#android
#Android
Вопрос:
Я пытаюсь создать линейную диаграмму в Android.Я использовал achartengine-1.1.0.jar.Но когда я пытаюсь запустить диаграмму, она не отображается.Пожалуйста, помогите. Вот код.
MainActivity.java
package com.example.linecharts;
import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class MainActivity extends ActionBarActivity {
private View mChart;
private String[] mMonth = new String[] {
"Jan", "Feb" , "Mar", "Apr", "May", "Jun",
"Jul", "Aug" , "Sep", "Oct", "Nov", "Dec"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openChart();
}
private void openChart()
{
int[] x = { 1,2,3,4,5,6,7,8 };
int[] income = { 2000,2500,2700,3000,2800,3500,3700,3800};
int[] expense = {2200, 2700, 2900, 2800, 2600, 3000, 3300, 3400 };
// Creating an XYSeries for Income
XYSeries incomeSeries = new XYSeries("Income");
// Creating an XYSeries for Expense
XYSeries expenseSeries = new XYSeries("Expense");
// Adding data to Income and Expense Series
for(int i=0;i<x.length;i )
{
incomeSeries.add(x[i], income[i]);
expenseSeries.add(x[i],expense[i]);
}
// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Income Series to the dataset
dataset.addSeries(incomeSeries);
// Adding Expense Series to dataset
dataset.addSeries(expenseSeries);
// Creating XYSeriesRenderer to customize incomeSeries
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.WHITE);
incomeRenderer.setPointStyle(PointStyle.CIRCLE);
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setDisplayChartValues(true);
// Creating XYSeriesRenderer to customize expenseSeries
XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
expenseRenderer.setColor(Color.YELLOW);
expenseRenderer.setPointStyle(PointStyle.CIRCLE);
expenseRenderer.setFillPoints(true);
expenseRenderer.setLineWidth(2);
expenseRenderer.setDisplayChartValues(true);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(0);
multiRenderer.setChartTitle("Income vs Expense Chart");
multiRenderer.setXTitle("Year 2012");
multiRenderer.setYTitle("Amount in Dollars");
multiRenderer.setZoomButtonsVisible(true);
for(int i=0;i<x.length;i )
{
multiRenderer.addXTextLabel(i 1, mMonth[i]);
}
// Adding incomeRenderer and expenseRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to multipleRenderer
// should be same
multiRenderer.addSeriesRenderer(incomeRenderer);
multiRenderer.addSeriesRenderer(expenseRenderer);
// Getting a reference to LinearLayout of the MainActivity Layout
LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);
// Creating a Line Chart
mChart = ChartFactory.getLineChartView(getBaseContext(), dataset, multiRenderer);
// Adding the Line Chart to the LinearLayout
chartContainer.addView(mChart);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main, container,
false);
return rootView;
}
}
}
activity_main.xml
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.linecharts.MainActivity$PlaceholderFragment">
<TextView
android:id="@ id/tv_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/str_tv_title"
android:gravity="center_horizontal" />
<LinearLayout
android:id="@ id/chart_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
ManifestFile.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.linecharts"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.linecharts.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Файл LogCat
06-16 00:31:12.640: E/AndroidRuntime(1282): FATAL EXCEPTION: main
06-16 00:31:12.640: E/AndroidRuntime(1282): Process: com.example.linecharts, PID: 1282
06-16 00:31:12.640: E/AndroidRuntime(1282): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.linecharts/com.example.linecharts.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
06-16 00:31:12.640: E/AndroidRuntime(1282): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-16 00:31:12.640: E/AndroidRuntime(1282): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-16 00:31:12.640: E/AndroidRuntime(1282): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-16 00:31:12.640: E/AndroidRuntime(1282): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-16 00:31:12.640: E/AndroidRuntime(1282): at android.os.Handler.dispatchMessage(Handler.java:102)
06-16 00:31:12.640: E/AndroidRuntime(1282): at android.os.Looper.loop(Looper.java:136)
06-16 00:31:12.640: E/AndroidRuntime(1282): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-16 00:31:12.640: E/AndroidRuntime(1282): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 00:31:12.640: E/AndroidRuntime(1282): at java.lang.reflect.Method.invoke(Method.java:515)
06-16 00:31:12.640: E/AndroidRuntime(1282): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-16 00:31:12.640: E/AndroidRuntime(1282): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-16 00:31:12.640: E/AndroidRuntime(1282): at dalvik.system.NativeStart.main(Native Method)
06-16 00:31:12.640: E/AndroidRuntime(1282): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
06-16 00:31:12.640: E/AndroidRuntime(1282): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111)
06-16 00:31:12.640: E/AndroidRuntime(1282): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
06-16 00:31:12.640: E/AndroidRuntime(1282): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
06-16 00:31:12.640: E/AndroidRuntime(1282): at com.example.linecharts.MainActivity.onCreate(MainActivity.java:32)
06-16 00:31:12.640: E/AndroidRuntime(1282): at android.app.Activity.performCreate(Activity.java:5231)
06-16 00:31:12.640: E/AndroidRuntime(1282): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-16 00:31:12.640: E/AndroidRuntime(1282): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-16 00:31:12.640: E/AndroidRuntime(1282): ... 11 more
Комментарии:
1.
You need to use a Theme.AppCompat theme (or descendant) with this activity.
2. в achartengine есть 2 способа отображения графика… через намерение и через графический вид … какой метод вам действительно нужен?
3. @mona: Как мы можем использовать графический вид?
4. вам необходимо преобразовать mChart = (GraphicalView)ChartFactory.getLineChartView(… параметры …);
Ответ №1:
Как показывает ваш Logcat, вы используете Actionbar из библиотеки поддержки. Из-за этого в вашем AndroidManifest.xml
использовании android:theme="@style/Theme.AppCompat"
в теге приложения.