我正在安装和测试本地html文件。这不起作用,我很遗憾地停了下来。然后我尝试了简单的活动,没有任何创造。 Btu仍然有同样的问题。没有应用程序没有安装.android:minSdkVersion="8“android:targetSdkVersion =”16“
05-13 10:09:49.325: D/dalvikvm(3136): Late-enabling CheckJNI
05-13 10:09:49.475: D/AndroidRuntime(3136): Shutting down VM
05-13 10:09:49.475: W/dalvikvm(3136): threadid=1: thread exiting with uncaught exception (group=0x414f42a0)
05-13 10:09:49.480: E/AndroidRuntime(3136): FATAL EXCEPTION: main
05-13 10:09:49.480: E/AndroidRuntime(3136): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testweb/com.example.testweb.MainActivity}: java.lang.NullPointerException
05-13 10:09:49.480: E/AndroidRuntime(3136): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
05-13 10:09:49.480: E/AndroidRuntime(3136): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
05-13 10:09:49.480: E/AndroidRuntime(3136): at android.app.ActivityThread.access$700(ActivityThread.java:140)
05-13 10:09:49.480: E/AndroidRuntime(3136): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
05-13 10:09:49.480: E/AndroidRuntime(3136): at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 10:09:49.480: E/AndroidRuntime(3136): at android.os.Looper.loop(Looper.java:137)
05-13 10:09:49.480: E/AndroidRuntime(3136): at android.app.ActivityThread.main(ActivityThread.java:4946)
05-13 10:09:49.480: E/AndroidRuntime(3136): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 10:09:49.480: E/AndroidRuntime(3136): at java.lang.reflect.Method.invoke(Method.java:511)
05-13 10:09:49.480: E/AndroidRuntime(3136): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
05-13 10:09:49.480: E/AndroidRuntime(3136): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
05-13 10:09:49.480: E/AndroidRuntime(3136): at dalvik.system.NativeStart.main(Native Method)
05-13 10:09:49.480: E/AndroidRuntime(3136): Caused by: java.lang.NullPointerException
05-13 10:09:49.480: E/AndroidRuntime(3136): at com.example.testweb.MainActivity.copyFile(MainActivity.java:55)
05-13 10:09:49.480: E/AndroidRuntime(3136): at com.example.testweb.MainActivity.onCreate(MainActivity.java:35)
05-13 10:09:49.480: E/AndroidRuntime(3136): at android.app.Activity.performCreate(Activity.java:5206)
05-13 10:09:49.480: E/AndroidRuntime(3136): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-13 10:09:49.480: E/AndroidRuntime(3136): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
05-13 10:09:49.480: E/AndroidRuntime(3136): ... 11 more
05-13 10:13:01.780: D/dalvikvm(3136): Debugger has detached; object registry had 1 entries
05-13 10:14:49.625: I/Process(3136): Sending signal. PID: 3136 SIG: 9
代码:
public class MainActivity extends Activity {
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
File f = copyFile(R.raw.index, "index.html");
File file = new File(f.getAbsolutePath());
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
Intent i = new Intent(Intent.ACTION_VIEW);
// i.setClassName("com.google.glass.browser", "com.google.glass.browser.WebBrowserActivity");
i.setClassName("com.google.android.browser", "com.android.browser.BrowserActivity");
i.setDataAndType(Uri.fromFile(file),mimetype);
mContext.startActivity(i);
}
private File copyFile(int resourceId, String filename) {
InputStream in = null;
OutputStream out = null;
File outFile = null;
try {
in = mContext.getResources().openRawResource(resourceId);
outFile = new File(mContext.getExternalFilesDir(null), filename);
Log.d("Test", "output file" + outFile.getAbsolutePath());
out = new FileOutputStream(outFile);
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
} catch(IOException e) {
Log.e("Test", "Failed to copy file", e);
} finally {
try {
in.close();
out.flush();
out.close();
in = null;
out = null;
} catch (Exception e){}
}
return outFile;
}
答案 0 :(得分:2)
试试这个..
mContext
未初始化尝试初始化..
mContext = this;
在onCreate
答案 1 :(得分:0)
您已安装了具有更高版本的相同应用程序,因为未安装较低版本的应用程序。
答案 2 :(得分:0)
您需要在mContext
中传递copyFile()
参数。
检查一下:
在onCreate()
内,执行此操作:File f = copyFile(R.raw.index, "index.html", mContext);
您也可以使用mContext
代替MainActivity.this
代替File f = copyFile(R.raw.index, "index.html", MainActivity.this);
。
private File copyFile(int resourceId, String filename, Context mContext) {
InputStream in = null;
OutputStream out = null;
File outFile = null;
try {
in = mContext.getResources().openRawResource(resourceId);
outFile = new File(mContext.getExternalFilesDir(null), filename);
Log.d("Test", "output file" + outFile.getAbsolutePath());
out = new FileOutputStream(outFile);
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
} catch(IOException e) {
Log.e("Test", "Failed to copy file", e);
} finally {
try {
in.close();
out.flush();
out.close();
in = null;
out = null;
} catch (Exception e){}
}
return outFile;
}
{{1}}
希望这有帮助。