Neal
Neal

Reputation: 141

FATAL Exception Main android

I am getting FATAL Exception main error.Any help?

Here is the code:

private void copyStream(String assetFilename, String outFileName ) throws 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");

}

And logcat

09-16 19:52:26.654: E/AndroidRuntime(294):FATAL EXCEPTION: main 09-16 19:52:26.654: E/AndroidRuntime(294):java.lang.IllegalStateException: Could not execute method of the activity 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/AndroidRuntime(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/AndroidRuntime(294):at dalvik.system.NativeStart.main(Native Method) 09-16 19:52:26.654: E/AndroidRuntime(294):Caused by: 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.reflect.Method.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 more 09-16 19:52:26.654: E/AndroidRuntime(294): Caused by: java.io.IOException: Broken pipe 09-16 19:52:26.654: E/AndroidRuntime(294): at org.apache.harmony.luni.platform.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:52:26.654: E/AndroidRuntime(294):at java.io.FileOutputStream.write(FileOutputStream.java:300) 09-16 19:52:26.654: E/AndroidRuntime(294):at java.io.FileOutputStream.write(FileOutputStream.java:256) 09-16 19:52:26.654: E/AndroidRuntime(294):at java.io.DataOutputStream.writeBytes(DataOutputStream.java:167)

Upvotes: 0

Views: 16451

Answers (5)

Deepak Vajpayee
Deepak Vajpayee

Reputation: 332

You are calling onClick method inside the thread. This is not possible as views are in main thread they can't be accessed from any other thread.

Upvotes: 0

Sadegh
Sadegh

Reputation: 2669

By default you don't have write permission on /system/etc, (unless you did some hack on your device). change it to Environment.getExternalStorageDirectory().getPath() + "/folderName/excluded-input-devices.xml"

Upvotes: 0

Luc
Luc

Reputation: 2805

Please using AsyncTask or Thread. If you wrote your own method, you can use like this:

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

OR using AsyncTask

Upvotes: 0

Rishabh Agrawal
Rishabh Agrawal

Reputation: 901

Add This Permission in your mainfest.I hope its work for you & u should initialize this object.In Your code you give the context value null thats why it is not working.

/** Required Some Intilization instead of null

ContextWrapper myContext = null;

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

Upvotes: 0

melodiouscode
melodiouscode

Reputation: 2069

The base of the exception appears to be coming from "FileOutputStream.write", have you added the write permissions to your manifest?

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

Upvotes: 0

Related Questions