帮助在 Android 中获取 HTML 文件

发布于 2024-10-19 13:05:27 字数 5021 浏览 2 评论 0原文

我正在编写一个在 Android 中获取 HTML 文件的程序,当我运行它时,我总是强制关闭!我不知道出了什么问题!以下是一些代码块:

将文件转换为字符串:

  public String getFileAsString(File file){ FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
        StringBuffer sb = new StringBuffer();
        try {
        fis = new FileInputStream(file);
        bis = new BufferedInputStream(fis);
        dis = new DataInputStream(bis);

        while (dis.available() != 0) {
        sb.append( dis.readLine() +"\n");
        }
        fis.close();
        bis.close();
        dis.close();

        } catch (FileNotFoundException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
        return sb.toString();
        }

Then where the problem might be:

    File htmlfile = null;
                EditText text = (EditText)findViewById(R.id.editText1);
                TextView tv =(TextView)findViewById(R.id.textView2);
                String data;
            URL url = null;
            try {
                url = new URL(text.getText().toString());
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                tv.setText("Invalid URL");
            }
            try {
                download(url,htmlfile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                tv.setText("Something went wrong try reinstalling the program!");
            }

            data = getFileAsString(htmlfile);
            tv.setText(data);

其他代码:

private static void download(URL input, File output)
    throws IOException {
  InputStream in = input.openStream();
  try {
    OutputStream out = new FileOutputStream(output);
    try {
      copy(in, out);
    } finally {
      out.close();
    }
  } finally {
    in.close();
  }
}

private static void copy(InputStream in, OutputStream out)
    throws IOException {
  byte[] buffer = new byte[1024];
  while (true) {
    int readCount = in.read(buffer);
    if (readCount == -1) {
      break;
    }
    out.write(buffer, 0, readCount);
  }
}


}

Logcat:

02-26 18:47:43.571: ERROR/AndroidRuntime(275): FATAL EXCEPTION: main
02-26 18:47:43.571: ERROR/AndroidRuntime(275): java.lang.NullPointerException: Argument must not be null
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at com.apps.blogspot.blogspot.getFileAsString(blogspot.java:40)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at com.apps.blogspot.blogspot.onClick(blogspot.java:83)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at android.view.View.performClick(View.java:2408)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at android.view.View$PerformClick.run(View.java:8816)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at android.os.Handler.handleCallback(Handler.java:587)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at android.os.Looper.loop(Looper.java:123)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at java.lang.reflect.Method.invokeNative(Native Method)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at java.lang.reflect.Method.invoke(Method.java:521)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at dalvik.system.NativeStart.main(Native Method)
02-26 18:47:43.661: WARN/ActivityManager(38):   Force finishing activity com.apps.blogspot/.blogspot
02-26 18:47:44.261: WARN/ActivityManager(38): Activity pause timeout for HistoryRecord{43e2ec30 com.apps.blogspot/.blogspot}
02-26 18:47:44.901: INFO/ARMAssembler(38): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x360050:0x36010c] in 6934332 ns
02-26 18:47:44.941: INFO/ARMAssembler(38): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x360110:0x3602d8] in 2187014 ns
02-26 18:47:48.831: INFO/Process(275): Sending signal. PID: 275 SIG: 9
02-26 18:47:48.871: INFO/ActivityManager(38): Process com.apps.blogspot (pid 275) has died.
02-26 18:47:48.871: INFO/WindowManager(38): WIN DEATH: Window{43fd01f8 com.apps.blogspot/com.apps.blogspot.blogspot paused=false}
02-26 18:47:48.911: WARN/InputManagerService(38): Got RemoteException sending setActive(false) notification to pid 275 uid 10036
02-26 18:47:55.766: WARN/ActivityManager(38): Activity destroy timeout for HistoryRecord{43e2ec30 com.apps.blogspot/.blogspot}

I am writing a program that gets a HTML file in Android and I keep getting a force close when I run it! I don't know what is wrong! Here are some blocks of code:

Converts file to string:

  public String getFileAsString(File file){ FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
        StringBuffer sb = new StringBuffer();
        try {
        fis = new FileInputStream(file);
        bis = new BufferedInputStream(fis);
        dis = new DataInputStream(bis);

        while (dis.available() != 0) {
        sb.append( dis.readLine() +"\n");
        }
        fis.close();
        bis.close();
        dis.close();

        } catch (FileNotFoundException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
        return sb.toString();
        }

Then where the problem might be:

    File htmlfile = null;
                EditText text = (EditText)findViewById(R.id.editText1);
                TextView tv =(TextView)findViewById(R.id.textView2);
                String data;
            URL url = null;
            try {
                url = new URL(text.getText().toString());
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                tv.setText("Invalid URL");
            }
            try {
                download(url,htmlfile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                tv.setText("Something went wrong try reinstalling the program!");
            }

            data = getFileAsString(htmlfile);
            tv.setText(data);

Other code:

private static void download(URL input, File output)
    throws IOException {
  InputStream in = input.openStream();
  try {
    OutputStream out = new FileOutputStream(output);
    try {
      copy(in, out);
    } finally {
      out.close();
    }
  } finally {
    in.close();
  }
}

private static void copy(InputStream in, OutputStream out)
    throws IOException {
  byte[] buffer = new byte[1024];
  while (true) {
    int readCount = in.read(buffer);
    if (readCount == -1) {
      break;
    }
    out.write(buffer, 0, readCount);
  }
}


}

Logcat:

02-26 18:47:43.571: ERROR/AndroidRuntime(275): FATAL EXCEPTION: main
02-26 18:47:43.571: ERROR/AndroidRuntime(275): java.lang.NullPointerException: Argument must not be null
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at com.apps.blogspot.blogspot.getFileAsString(blogspot.java:40)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at com.apps.blogspot.blogspot.onClick(blogspot.java:83)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at android.view.View.performClick(View.java:2408)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at android.view.View$PerformClick.run(View.java:8816)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at android.os.Handler.handleCallback(Handler.java:587)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at android.os.Looper.loop(Looper.java:123)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at java.lang.reflect.Method.invokeNative(Native Method)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at java.lang.reflect.Method.invoke(Method.java:521)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at dalvik.system.NativeStart.main(Native Method)
02-26 18:47:43.661: WARN/ActivityManager(38):   Force finishing activity com.apps.blogspot/.blogspot
02-26 18:47:44.261: WARN/ActivityManager(38): Activity pause timeout for HistoryRecord{43e2ec30 com.apps.blogspot/.blogspot}
02-26 18:47:44.901: INFO/ARMAssembler(38): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x360050:0x36010c] in 6934332 ns
02-26 18:47:44.941: INFO/ARMAssembler(38): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x360110:0x3602d8] in 2187014 ns
02-26 18:47:48.831: INFO/Process(275): Sending signal. PID: 275 SIG: 9
02-26 18:47:48.871: INFO/ActivityManager(38): Process com.apps.blogspot (pid 275) has died.
02-26 18:47:48.871: INFO/WindowManager(38): WIN DEATH: Window{43fd01f8 com.apps.blogspot/com.apps.blogspot.blogspot paused=false}
02-26 18:47:48.911: WARN/InputManagerService(38): Got RemoteException sending setActive(false) notification to pid 275 uid 10036
02-26 18:47:55.766: WARN/ActivityManager(38): Activity destroy timeout for HistoryRecord{43e2ec30 com.apps.blogspot/.blogspot}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦太阳 2024-10-26 13:05:27

htmlfilenull:您从未为其赋值。

以下是从堆栈跟踪中得出的结果:

02-26 18:47:43.571: ERROR/AndroidRuntime(275): java.lang.NullPointerException: Argument must not be null
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at com.apps.blogspot.blogspot.getFileAsString(blogspot.java:40)

从跟踪的顶部,您会看到在 getFileAsString 中构造一个 FileInputStreamNullPointerException代码>方法。

肯定是参数filenull,所以寻找你看到的调用者:

data = getFileAsString(htmlfile);

并寻找一个给htmlfile赋值的地方,你只会发现:

File htmlfile = null;

htmlfile is null: you never assign a value to it.

Here's how you figure it out from the stacktrace:

02-26 18:47:43.571: ERROR/AndroidRuntime(275): java.lang.NullPointerException: Argument must not be null
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
02-26 18:47:43.571: ERROR/AndroidRuntime(275):     at com.apps.blogspot.blogspot.getFileAsString(blogspot.java:40)

From the top of the trace, you see you've got a NullPointerException constructing a FileInputStream in your getFileAsString method.

It must be that the parameter file is null, so looking for the caller you see:

data = getFileAsString(htmlfile);

And looking for a place where something is assigned to htmlfile, you only find:

File htmlfile = null;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文