您好我正在尝试使用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
感谢您的帮助。
答案 0 :(得分:1)
Context
以Activity
方式开头,onCreate()
。如果您将GraphView
初始化为字段,则代码将失败,因为Context
尚不可用(您不应该像Views
一样初始化GraphView
。所以而不是:
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来添加库(请参阅this answer)。