0

嗨,我正在尝试使用 JJOE64 的 GraphView 库,我使用 eclipse“添加外部档案”在我的项目中导入了 .jar 文件。

我创建了一个新的 GrapVew 对象

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");


}

我玩了这个应用程序看看会发生什么,我得到了这个致命错误

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

感谢帮助。

4

1 回答 1

1

Context可以从方法Activity开始onCreate()。如果您将 初始化GraphView为一个字段,您的代码将失败,因为Context尚不可用(您永远不应该像使用 那样进行初始化ViewsGraphView。所以而不是:

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

做这个:

GraphView Grafico;

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

GraphView此外,您应该通过直接使用项目或直接在libs文件夹中使用 jar来添加库(请参阅此答案)。

于 2013-03-03T15:47:57.287 回答