Вопрос

Hi i'm trying to use GraphView library by JJOE64, i imported the .jar file in my project using eclipse "Add external archives".

I created a new GrapVew Object

package com.example.graphtest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import com.jjoe64.graphview.*;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @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;
    }

    GraphView Grafico = new LineGraphView(this, "GraphTest");


}

and i played the app to see what happens and i got this fatal error

03-03 15:22:11.863: E/Trace(1048): error opening trace file: No such file or directory (2)
03-03 15:22:12.063: D/AndroidRuntime(1048): Shutting down VM
03-03 15:22:12.083: W/dalvikvm(1048): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-03 15:22:12.093: E/AndroidRuntime(1048): FATAL EXCEPTION: main
03-03 15:22:12.093: E/AndroidRuntime(1048): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.graphdraw/com.example.graphdraw.MainActivity}: java.lang.NullPointerException
03-03 15:22:12.093: E/AndroidRuntime(1048):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at android.os.Looper.loop(Looper.java:137)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at java.lang.reflect.Method.invokeNative(Native Method)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at java.lang.reflect.Method.invoke(Method.java:511)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at dalvik.system.NativeStart.main(Native Method)
03-03 15:22:12.093: E/AndroidRuntime(1048): Caused by: java.lang.NullPointerException
03-03 15:22:12.093: E/AndroidRuntime(1048):     at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at android.view.View.<init>(View.java:3226)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at android.view.ViewGroup.<init>(ViewGroup.java:420)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at android.widget.LinearLayout.<init>(LinearLayout.java:168)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at com.jjoe64.graphview.GraphView.<init>(GraphView.java:258)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at com.jjoe64.graphview.LineGraphView.<init>(LineGraphView.java:20)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at com.example.graphdraw.MainActivity.<init>(MainActivity.java:27)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at java.lang.Class.newInstanceImpl(Native Method)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at java.lang.Class.newInstance(Class.java:1319)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-03 15:22:12.093: E/AndroidRuntime(1048):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-03 15:22:12.093: E/AndroidRuntime(1048):     ... 11 more
03-03 15:22:15.763: I/Process(1048): Sending signal. PID: 1048 SIG: 9

Thanks for help.

Это было полезно?

Решение

The Context is available in an Activity starting with the onCreate() method. If you initialize the GraphView as a field your code will fail as the Context is not available yet(you should never initialize Views as you do with the GraphView). So instead of:

GraphView Grafico = new LineGraphView(this, "GraphTest");

do this:

GraphView Grafico;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Grafico = new LineGraphView(this, "GraphTest"); 
}

Also, you should add the library by either using the GraphView project directly or by using the jar directly in the libs folder(see this answer).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top