2

While i am debugging my application it crash. I am getting the message "unfortunately XXX has stopped error". The application reading files from SD.

This is the Logcat message:

01-15 22:08:49.878  20190-20190/com.example.ReadFile I/System.out﹕ debugger has settled (1378)
01-15 22:08:49.962  20190-20209/com.example.ReadFile D/OpenGLRenderer﹕ Render dirty regions requested: true
01-15 22:08:49.966  20190-20190/com.example.ReadFile D/Atlas﹕ Validating map...
01-15 22:08:50.046  20190-20209/com.example.ReadFile I/OpenGLRenderer﹕ Initialized EGL, version 1.4
01-15 22:08:50.068  20190-20209/com.example.ReadFile D/OpenGLRenderer﹕ Enabling debug mode 0
01-15 22:08:50.070  20190-20209/com.example.ReadFile D/mali_winsys﹕ new_window_surface returns 0x3000
01-15 22:08:58.528  20190-20190/com.example.ReadFile I/Choreographer﹕ Skipped 129 frames!  The application may be doing too much work on its main thread.
01-15 22:09:11.207  20190-20198/com.example.ReadFile A/libc﹕ Fatal signal 11 (SIGSEGV), code 1, fault addr 0xdead1234 in tid 20198 (JDWP)

Here is the code:

This is the Activity:

public class MyActivity extends Activity {

    private UdpClient mUdpClient;
    private TextView txtTest;
    private Button btnDisconnect;
    private Button btnConnect;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        txtTest = (TextView)findViewById(R.id.txtTest);
        btnConnect = (Button)findViewById(R.id.btnConnect);

        btnConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ConnectToServer();
            }
        });
    }

    private void ConnectToServer() {
        new ConnectTask().execute("");
    }
    public class ConnectTask extends AsyncTask<String, Bitmap, UdpClient> {
        @Override
        protected UdpClient doInBackground(String... message) {

            mUdpClient = new UdpClient(getApplicationContext());
            mUdpClient.run();
            return null;
        }

        @Override
        protected void onProgressUpdate(Bitmap... bitmap){
            super.onProgressUpdate(bitmap);
        }
    }
}

This is the code that read the files:

public class UdpClient {
    public static Context ctx;


    public UdpClient(Context context) {
        ctx = context;
    }

    public void run()
    {
        int valueIndex = R.raw.image_nearest;
        int imageXYs = R.raw.image_xy;

        String[] strNearestValue = this.ReadValueFile(valueIndex);
        String[] imageXY = ReadValueFile(imageXYs);

        int []nearestValue = new int[strNearestValue.length];

        int numOfPixel = imageXY.length;
        int []pixels = new int[numOfPixel];

        for(int i=0;i<imageXY.length;i++)
        {
            nearestValue[i] = Integer.parseInt(strNearestValue[i]);
            pixels[i]=GetXY(imageXY[i]);
        }
    }

    public static int GetXY(String xyRaw) {
        String[] xy = xyRaw.split(",");
        short xPixel = Short.parseShort(xy[0]);
        short yPixel = Short.parseShort(xy[1]);
        int pixelIndex = 800 * (yPixel - 1) + xPixel - 1;
        return pixelIndex;
    }


    private String[] ReadValueFile(int fileResourceID) {
        List<String> list = new ArrayList<String>();
        try(InputStream inputStream = ctx.getResources().openRawResource(fileResourceID);){
            try(BufferedReader br= new BufferedReader(new InputStreamReader(inputStream)))
            {
                String line;
                while ((line = br.readLine()) != null) {
                    list.add(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list.toArray(new String[list.size()]);
    }
}

This is the stacktrace:

01-15 22:02:00.884  19747-19847/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.example.ReadFile, PID: 19747
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.ReadFile.UdpClient.run()' on a null object reference
            at com.example.ReadFile.MyActivity$ConnectTask.doInBackground(MyActivity.java:55)
            at com.example.ReadFile.MyActivity$ConnectTask.doInBackground(MyActivity.java:51)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
01-15 22:02:37.108  19870-19901/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.example.ReadFile, PID: 19870
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.ReadFile.UdpClient.run()' on a null object reference
            at com.example.ReadFile.MyActivity$ConnectTask.doInBackground(MyActivity.java:55)
            at com.example.ReadFile.MyActivity$ConnectTask.doInBackground(MyActivity.java:51)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
01-15 22:08:16.826      399-442/? E/InputDispatcher﹕ channel '2f43cf57 com.example.ReadFile/com.example.ReadFile.MyActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
01-15 22:35:42.096      399-442/? E/InputDispatcher﹕ channel '23d7860 com.example.ReadFile/com.example.ReadFile.MyActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
01-15 22:58:22.366      399-442/? E/InputDispatcher﹕ channel '14d3e113 com.example.ReadFile/com.example.ReadFile.MyActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
01-15 23:02:44.483  21264-21272/com.example.ReadFile A/libc﹕ Fatal signal 11 (SIGSEGV), code 1, fault addr 0xdead1234 in tid 21272 (JDWP)
01-15 23:02:45.046      399-442/? E/InputDispatcher﹕ channel '16edc311 com.example.ReadFile/com.example.ReadFile.MyActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

The program crash once i read the files.

dan
  • 169
  • 1
  • 3
  • 11

2 Answers2

0

Looks to me that the main CPU is being overworked, In other words, it is much to resource intensive, For example when a file stops responding and crashes in windows, It is basicly the same thing, try optimizing it or spreading out the thread.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
Byte Code
  • 7
  • 2
  • 12
0

Log says that mUdpClient is null. Try initializing mUdpClient before calling AsyncTask:

mUdpClient = new UdpClient(getApplicationContext());

Also try making mUdpClient public.

toidiu
  • 965
  • 2
  • 12
  • 25
  • So the bad thing about AsyncTasks is that they dont support context change(screen rotation and other similar changes). Could you run the app again and make sure rotation is turned off. Also Could you add a breakpoint to see if mUdpClient is null or not before calling mUdpClient.run(); – toidiu Jan 16 '15 at 19:13