伙计们,我在 android 中运行时遇到问题,但在 java 中运行时没有问题。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.txtview);
Button bt = (Button) findViewById(R.id.button1);
am = this.getAssets();
bt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
try {
parsePdf("android.resource://com.example.panalyzer_v1/raw/resume.pdf","android.resource://com.example.panalyzer_v1/raw/resume.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
);
}
public void parsePdf(String pdf, String txt) throws IOException {
PdfReader reader = new PdfReader(pdf);
PrintWriter out = new PrintWriter(new FileOutputStream(txt));
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
out.println(PdfTextExtractor.getTextFromPage(reader, i));
}
out.flush();
out.close();
}
此代码将提取 PDF 中的整个文本并将其传输到文本文件中,但我不知道 Android 是否可以做到这一点。我认为我的问题是文件路径:parsePdf("assets/Resume.pdf","assets/Resume.txt");
我无法使其工作。
我已经更改了路径,但错误没有解决。我调试了它,我仍然得到一个错误:
10-22 20:16:13.850: E/AndroidRuntime(657): FATAL EXCEPTION: main
10-22 20:16:13.850: E/AndroidRuntime(657): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.panalyzerdemo/com.example.panalyzerdemo.MainActivity}: java.lang.NullPointerException
10-22 20:16:13.850: E/AndroidRuntime(657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.os.Looper.loop(Looper.java:123)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-22 20:16:13.850: E/AndroidRuntime(657): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 20:16:13.850: E/AndroidRuntime(657): at java.lang.reflect.Method.invoke(Method.java:521)
10-22 20:16:13.850: E/AndroidRuntime(657): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-22 20:16:13.850: E/AndroidRuntime(657): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-22 20:16:13.850: E/AndroidRuntime(657): at dalvik.system.NativeStart.main(Native Method)
10-22 20:16:13.850: E/AndroidRuntime(657): Caused by: java.lang.NullPointerException
10-22 20:16:13.850: E/AndroidRuntime(657): at com.example.panalyzerdemo.MainActivity.onCreate(MainActivity.java:36)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-22 20:16:13.850: E/AndroidRuntime(657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-22 20:16:13.850: E/AndroidRuntime(657): ... 11 more
10-22 20:16:16.831: I/Process(657): Sending signal. PID: 657 SIG: 9
我知道问题所在PdfReader reader = new PdfReader(pdf);
。我走错了吗?
PS:在调试方面,我是新手。如果它是错误的调试报告,请纠正我。
谢谢你。