我想在Android的Graph View中实时显示设备使用的数据

时间:2015-05-06 09:45:53

标签: android android-graphview

我想在Android的Graph View中实时显示设备使用的数据。数据应以图表形式显示。如果有的话,Plz提供代码。

我在Graph Class中使用此代码但在运行时使用Drops(bug)。

getApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
public class Graph extends Fragment {

private final Handler mHandler = new Handler();
private Runnable mTimer1;
private Runnable mTimer2;
private LineGraphSeries<DataPoint> mSeries1;
private LineGraphSeries<DataPoint> mSeries2;
private double graph2LastXValue = 5d;

@SuppressLint("CutPasteId")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.graph, container, false);

    GraphView graph = (GraphView) rootView.findViewById(R.id.graph);
    mSeries1 = new LineGraphSeries<DataPoint>(generateData());
    graph.addSeries(mSeries1);

    GraphView graph2 = (GraphView) rootView.findViewById(R.id.graph2);
    mSeries2 = new LineGraphSeries<DataPoint>();
    graph2.addSeries(mSeries2);
    graph2.getViewport().setXAxisBoundsManual(true);
    graph2.getViewport().setMinX(0);
    graph2.getViewport().setMaxX(40);

    return rootView;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    ((MainActivity) activity).onSectionAttached(getArguments().getInt(
            MainActivity.ARG_SECTION_NUMBER));
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
public void onResume() {
    super.onResume();
    mTimer1 = new Runnable() {
        @Override
        public void run() {
            mSeries1.resetData(generateData());
            mHandler.postDelayed(this, 300);
        }
    };
    mHandler.postDelayed(mTimer1, 300);

    mTimer2 = new Runnable() {
        @Override
        public void run() {
            graph2LastXValue += 1d;
            mSeries2.appendData(
                    new DataPoint(graph2LastXValue, getRandom()), true, 40);
            mHandler.postDelayed(this, 200);
        }
    };
    mHandler.postDelayed(mTimer2, 1000);
}

@Override
public void onPause() {
    mHandler.removeCallbacks(mTimer1);
    mHandler.removeCallbacks(mTimer2);
    super.onPause();
}

private DataPoint[] generateData() {
    int count = 30;
    DataPoint[] values = new DataPoint[count];
    for (int i = 0; i < count; i++) {
        double x = i;
        double f = mRand.nextDouble() * 0.15 + 0.3;
        double y = Math.sin(i * f + 2) + mRand.nextDouble() * 0.3;
        DataPoint v = new DataPoint(x, y);
        values[i] = v;
    }
    return values;
}

double mLastRandom = 2;
Random mRand = new Random();

private double getRandom() {
    return mLastRandom += mRand.nextDouble() * 0.5 - 0.25;
}
}

这是我的LogCat。

05-06 15:21:47.014: E/AndroidRuntime(30815): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.totaldata/com.example.totaldata.Graph}: java.lang.ClassCastException: com.example.totaldata.Graph cannot be cast to android.app.Activity
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1995)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:706)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.widget.TabHost.setCurrentTab(TabHost.java:350)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:540)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.view.View.performClick(View.java:3538)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.view.View$PerformClick.run(View.java:14330)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.os.Handler.handleCallback(Handler.java:608)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.os.Looper.loop(Looper.java:156)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.app.ActivityThread.main(ActivityThread.java:4987)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at java.lang.reflect.Method.invokeNative(Native Method)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at java.lang.reflect.Method.invoke(Method.java:511)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at dalvik.system.NativeStart.main(Native Method)
05-06 15:21:47.014: E/AndroidRuntime(30815): Caused by: java.lang.ClassCastException: com.example.totaldata.Graph cannot be cast to android.app.Activity
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.app.Instrumentation.newActivity(Instrumentation.java:1039)
05-06 15:21:47.014: E/AndroidRuntime(30815):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)

我的宣言......

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.totaldata"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.totaldata.Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.example.totaldata.MainActivity" />
    <activity android:name="com.example.totaldata.Graph" />
    <activity android:name="com.example.totaldata.ApplicationRunning" />
</application>

0 个答案:

没有答案