2012-09-16 123 views
1

我越來越致命異常主要error.Any幫助嗎?致命異常主要android

下面是代碼:

私人無效copyStream(字符串assetFilename,字符串outFileName) 拋出IOException異常 {

 Process mSuProcess; 
     mSuProcess = Runtime.getRuntime().exec("su"); 
     ContextWrapper myContext = null; 
        InputStream myInput = myContext.getAssets().open(assetFilename); 

     OutputStream myOutput = new FileOutputStream(outFileName); 

     //transfer bytes from the inputfile to the outputfile 
     byte[] buffer = new byte[2048]; 
     int length; 
     while ((length = myInput.read(buffer))>0){ 
      myOutput.write(buffer, 0, length); 
     } 

     //Close the streams 
     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 

    } 


public void copyon(View view) throws IOException 
{ 
    copyStream("floaton.xml","/system/etc/excluded-input-devices.xml"); 

} 

public void copyoff(View view) throws IOException 
{ 
copyStream("floatoff.xml","/system/etc/excluded-input-devices.xml"); 

} 

而且logcat的

09-16 19:52:26.654:E/AndroidRuntime(294):致命例外:main 0 9-16 19:52:26.654 E/AndroidRuntime(294):java.lang.IllegalStateException: 無法執行活動的方法09-16 19:52:26.654: E/AndroidRuntime(294):at android .view.View $ 1.onClick(View.java:2144) 09-16 19:52:26.654:E/AndroidRuntime(294):at android.view.View.performClick(View.java:2485)09-16 19/52:26.654: E/AndroidRuntime(294):at android.view.View $ PerformClick.run(View.java:9080)09-16 19:52:26.654: E/AndroidRuntime(294):at android.os.Handler.handleCallback(Handler.java:587)09-16 19:52:26.654:E/AndroidRuntime(294):at android.os.Handler.dispatchMessage(Handler.java:92)09- 16 19:52:26.654:E/AndroidR (294):at android.os.Looper.loop(Looper.java:123)09-16 19:52:26.654: E/AndroidRuntime(294):at android.app.ActivityThread.main(ActivityThread。 java:3683)09-16 19:52:26.654:E/AndroidRuntime(294):at java.lang.reflect.Method.invokeNative(Native Method)09-16 19:52:26.654:E/AndroidRuntime 294):at java.lang.reflect.Method.invoke(Method.java:507)09-16 19:52:26.654: E/AndroidRuntime(294):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 09-16 19:52:26.654:E/AndroidRuntime(294):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)09-16 19:52:26.654:E/AndroidRuntim e(294):at dalvik.system.NativeStart.main(本機方法)09-16 19:52:26.654: E/AndroidRuntime(294):導致: java.lang.reflect.InvocationTargetException 09-16 19 :52:26.654: E/AndroidRuntime(294):at java.lang.reflect.Method.invokeNative(Native Method)09-16 19:52:26.654:E/AndroidRuntime(294):at java.lang。反射。方法.invoke(Method.java:507)09-16 19:52:26.654: E/AndroidRuntime(294):at android.view.View $ 1.onClick(View.java:2139) 09-16 19 :52:26.654:E/AndroidRuntime(294):... 11更多09-16 19:52:26.654:E/AndroidRuntime(294):引起:java.io.IOException: 斷開的管道09-16 19 :52:26.654:E/AndroidRuntime(294):at org.apache.harmony.luni.pl atform.OSFileSystem.write(Native Method) 09-16 19:52:26.654:E/AndroidRuntime(294):at dalvik.system.BlockGuard $ WrappedFileSystem.write(BlockGuard.java:171) 09-16 19: E:/ AndroidRuntime(294):at java.io.FileOutputStream.write(FileOutputStream.java:300)09-16 19:52:26.654: FileOutputStream.write(FileOutputStream.java:256)09-16 19:52:26.654:E/AndroidRuntime(294):at java.io.DataOutputStream.writeBytes(DataOutputStream。java:167)

+0

我面臨同樣的問題..但如果我將「su」更改爲「sh」 那麼它就起作用(至少不會引發破管異常) – DeltaCap

回答

0

異常的基礎看起來是來自「FileOutputStream.write」,您是否向清單中添加了寫入權限?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+0

已添加權限,但仍存在錯誤。 – Neal

0

添加此權限在mainfest.I希望它爲你工作&ü應該初始化這個object.In你的代碼,你給它爲什麼不工作的情況下null值多數民衆贊成。

/**需要某種Intilization代替空

ContextWrapper myContext = NULL;

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+0

添加了權限,但仍存在錯誤。 – Neal

+0

/**需要一些Intilization而不是null ContextWrapper myContext = null; – yokees

+0

而不是ContextWrapper myContext = null,我做了私人ContextWrapper myContext;甚至刪除myContext,仍然沒有解決方案。 – Neal

0

請使用AsyncTask或Thread。 如果你寫你自己的方法,你可以使用這樣的:

new Thread(
    new Runnable() { 
     @Override 
     public void run() { 
      copyStream(); 
     } 
    } 
); 

或使用的AsyncTask

0

默認情況下,你沒有寫權限在/system/etc,(除非你做一些黑客你設備)。將其更改爲Environment.getExternalStorageDirectory().getPath() + "/folderName/excluded-input-devices.xml"