我的Android应用程序崩溃的原因

时间:2015-01-15 20:25:16

标签: android android-asynctask crash

我正在调试我的应用程序时崩溃了。 我收到了消息"不幸的是XXX已经停止了错误"。 应用程序从SD读取文件。

这是Logcat消息:

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)

以下是代码:

这是活动:

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

这是读取文件的代码:

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()]);
    }
}

这是堆栈跟踪:

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!

一旦我读完文件,程序就会崩溃。

2 个答案:

答案 0 :(得分:0)

在我看来,主CPU过度工作,换句话说,它是资源密集型的,例如当一个文件停止响应并在Windows中崩溃时,基本上是相同的,尝试优化它或展开线程。

答案 1 :(得分:0)

Log说mUdpClient为null。 尝试在调用AsyncTask之前初始化mUdpClient:

mUdpClient = new UdpClient(getApplicationContext());

还要尝试将mUdpClient公开。