2014-05-13 180 views
-1

我正在安裝和測試本地html文件。這不行,我不幸停了下來。然後,我嘗試了簡單的活動,沒有任何創建。 Btu仍然是我得到同樣的問題。應用無不是installing.android:minSdkVersion="8" 機器人:targetSdkVersion =‘16’apk不安裝

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

您有一個'NullPointerException' @ line 55'MainActivity.java' – Raghunandan

+0

請發佈您的MainActivity.java代碼。錯誤在55行和35行。 –

+0

檢查你的清單文件。 @siddharth:如何檢測哪些行是錯誤?像你說的55和35 –

回答

2

試試這個..

mContext未初始化嘗試初始化它..

mContext = this; 

onCreate

+0

獲取相同的錯誤。 – user3602987

+0

@ user3602987你在哪裏添加該行? – Hariharan

+0

上下文mContext = this; – user3602987

0

您已經安裝了應有更高版本的同一應用程序沒有安裝低版本的應用程序。

+0

我認爲我的代碼崩潰了....你能檢查我的代碼是否正確? – user3602987

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; 
} 

希望這有助於。