setText导致崩溃

时间:2012-03-04 06:17:11

标签: java android

对于过去一周中每天的大部分时间,我一直致力于通过电位器的输入来控制文本字段的项目,并且我的setText命令不断使程序崩溃。我厌倦了修补和调试,但收效甚微,我需要一些帮助。

如果您需要查看所有项目文件,我也可以上传它们。

搜索“此行导致崩溃”

package edu.uidaho.pong;

import java.io.IOException;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
import android.app.Activity;

import android.view.SurfaceHolder;

public class GameThread extends Thread {


    /** Handle to the surface manager object we interact with */
    private SurfaceHolder _surfaceHolder;
    private Paint _paint;
    private GameState _state;
    public PongActivity _pong;

    public GameThread(SurfaceHolder surfaceHolder, Context context,
            Handler handler) {
        _surfaceHolder = surfaceHolder;
        _paint = new Paint();
        _state = new GameState();
        _pong = new PongActivity();

    }

//  @Override
    public void run() {
        while (true) {
            Canvas canvas = _surfaceHolder.lockCanvas();
            _state.update(_pong);
            _state.draw(canvas, _paint);
            _surfaceHolder.unlockCanvasAndPost(canvas);


            int ret = 0;
            byte[] buffer = new byte[16384];
            int i;

            int tempdebug = 0;
            //while (true) { // read data
            while (tempdebug++ < 10){
//              try {
//                  ret = _pong.mInputStream.read(buffer);
//              } catch (IOException e) {
//                  break;
//              }
                ret = 5; // let's pretend there's a 5 every time since I can't test the usb

                i = 0;
                while (i < ret) {
                    int len = ret - i;
                    if (len >= 1) {
                        Message m = Message.obtain(_pong.mHandler);
                        int value = (int)buffer[i];
                        // 'f' is the flag, use for your own logic
                        // value is the value from the arduino
                        m.obj = new ValueMsg('f', value);
                        _pong.mHandler.sendMessage(m);
                    }
                    i += 1; // number of bytes sent from arduino
                }
            }
            //_pong.mResponseField.setText("Reading: "); This line causes a crash

        }
    }

    public GameState getGameState() {
        return _state;
    }
}

Logcat输出 -

03-03 22:45:49.639: W/dalvikvm(381): threadid=9: thread exiting with uncaught exception (group=0x40015560)
03-03 22:45:49.639: E/AndroidRuntime(381): FATAL EXCEPTION: Thread-10
03-03 22:45:49.639: E/AndroidRuntime(381): java.lang.NullPointerException
03-03 22:45:49.639: E/AndroidRuntime(381):  at edu.uidaho.pong.GameThread.run(GameThread.java:70)
03-03 22:45:50.189: E/global(381): Deprecated Thread methods are not supported.
03-03 22:45:50.189: E/global(381): java.lang.UnsupportedOperationException
03-03 22:45:50.189: E/global(381):  at java.lang.VMThread.stop(VMThread.java:85)
03-03 22:45:50.189: E/global(381):  at java.lang.Thread.stop(Thread.java:1280)
03-03 22:45:50.189: E/global(381):  at java.lang.Thread.stop(Thread.java:1247)
03-03 22:45:50.189: E/global(381):  at edu.uidaho.pong.GameView.surfaceDestroyed(GameView.java:46)
03-03 22:45:50.189: E/global(381):  at android.view.SurfaceView.reportSurfaceDestroyed(SurfaceView.java:587)
03-03 22:45:50.189: E/global(381):  at android.view.SurfaceView.updateWindow(SurfaceView.java:481)
03-03 22:45:50.189: E/global(381):  at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:213)
03-03 22:45:50.189: E/global(381):  at android.view.View.dispatchWindowVisibilityChanged(View.java:4027)
03-03 22:45:50.189: E/global(381):  at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
03-03 22:45:50.189: E/global(381):  at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
03-03 22:45:50.189: E/global(381):  at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
03-03 22:45:50.189: E/global(381):  at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:720)
03-03 22:45:50.189: E/global(381):  at android.view.ViewRoot.performTraversals(ViewRoot.java:782)
03-03 22:45:50.189: E/global(381):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
03-03 22:45:50.189: E/global(381):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 22:45:50.189: E/global(381):  at android.os.Looper.loop(Looper.java:130)
03-03 22:45:50.189: E/global(381):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-03 22:45:50.189: E/global(381):  at java.lang.reflect.Method.invokeNative(Native Method)
03-03 22:45:50.189: E/global(381):  at java.lang.reflect.Method.invoke(Method.java:507)
03-03 22:45:50.189: E/global(381):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-03 22:45:50.189: E/global(381):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-03 22:45:50.189: E/global(381):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

我不能确切地说因为没有logcat,但这可能正在发生,因为你试图从单独的线程更改你的TextView,这在Android中被禁止。解决此问题的方法之一是在运行单独线程的类中引用Activity,并在View方法中放置任何Activity.runOnUiThread()更改代码,该方法需要{ {1}}作为论据。希望这会有所帮助。

修改

Runnable