简体   繁体   中英

Display Freezes, Force Close or Wait, and GC_MALLOC and GC_Concurrent are Triggered

I am trying to use the Thread to process the data in real time to store 8000 strings in a multi-dimensional array. The logic for storing the array has been tested with a string 8000 numbers, that was successful. There are two variations for this code the catch (NumberFormatException e) and the catch(NullPointerException e). The logcat for both is listed below. Then it crashes. How do I fix this?

Changing transfer = readMessage; and setting the null to zero did prevent the errors. However, my UI still gets frozen and I get the Force Close or Wait option. Furthermore, GC_MALLOC and GC_Concurrent are appearing in the error log now.

    public double [][]stored = new double[8000][1];
    public static HjorthClass finalValue;
    public String transfer;


    class Task implements Runnable
    {
        @Override
        public void run() 
        {
            try{
                Thread.sleep(2000);
               }
               catch(InterruptedException e)
                {   
                }

                  for(int a = 0; a<8000; a++)
                {
                        try
                        {
                          double[] convert = new double[1];
                          for(int z=0; z <1;z++)
                              {
                             convert[z]= Double.parseDouble(transfer);
                          }
                          for(int j=0; j<1;j++)
                          {
                             stored[a][j]= convert[j];
                          }
                        }
                        catch(NumberFormatException e)
                        {
                        }

               }


            finalValue = new HjorthClass(stored);
        }}


    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(final Message msg) {
            switch (msg.what) {

            case MESSAGE_READ:
            byte[] readBuf = (byte[]) msg.obj;
                String readMessage = new String(readBuf,0,msg.arg1);
                String transfer = readMessage;
                mConversationArrayAdapter.add("Voltage: "+ transfer);
                new Thread(new Task()).start();
                break;

logcat with catch(NumberFormatException e)

04-07 06:04:01.712: E/AndroidRuntime(24162): FATAL EXCEPTION: Thread-14
04-07 06:04:01.712: E/AndroidRuntime(24162): java.lang.NullPointerException
04-07 06:04:01.712: E/AndroidRuntime(24162):    at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:263)
04-07 06:04:01.712: E/AndroidRuntime(24162):    at java.lang.Double.parseDouble(Double.java:318)
04-07 06:04:01.712: E/AndroidRuntime(24162):    at com.example.android.BluetoothChat.BluetoothChat$Task.run(BluetoothChat.java:248)
04-07 06:04:01.712: E/AndroidRuntime(24162):    at java.lang.Thread.run(Thread.java:1019)

logcat with catch(NullPointerException e)

04-07 05:51:55.342: E/AndroidRuntime(23838): FATAL EXCEPTION: Thread-181
04-07 05:51:55.342: E/AndroidRuntime(23838): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
04-07 05:51:55.342: E/AndroidRuntime(23838):    at android.os.Handler.<init>(Handler.java:121)
04-07 05:51:55.342: E/AndroidRuntime(23838):    at android.app.Activity.<init>(Activity.java:680)
04-07 05:51:55.342: E/AndroidRuntime(23838):    at com.example.android.BluetoothChat.HjorthClass.<init>(HjorthClass.java:18)
04-07 05:51:55.342: E/AndroidRuntime(23838):    at com.example.android.BluetoothChat.BluetoothChat$Task.run(BluetoothChat.java:263)
04-07 05:51:55.342: E/AndroidRuntime(23838):    at java.lang.Thread.run(Thread.java:1019)

I think the problem is transfer is null, You can put a condition like this before convert: (the first question which you edit and change your topic)

if(transfer ==null)
transfer="0";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM