1

所以当一个动作附加到另一个线程中时,我有这个代码来更改 texview 中的文本:

编辑:我更改了答案中所说的代码:

public class TargetActivity extends Activity {

  TextView Text = null;
  private Handler mHandler;

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

       Text = (TextView) findViewById(R.id.textView1);

       mHandler = new Handler();
   }

   public void receiveMyMessage() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {

                if (Text != null) Text.setText("Done");
            }
        });
    }
}

现在我有这些错误:

01-06 13:57:13.656: E/AndroidRuntime(25361): FATAL EXCEPTION: Thread-9
01-06 13:57:13.656: E/AndroidRuntime(25361): java.lang.NullPointerException
01-06 13:57:13.656: E/AndroidRuntime(25361):    at com.mat.archery.statistics.TargetActivity.receiveMyMessage(TargetActivity.java:30)
01-06 13:57:13.656: E/AndroidRuntime(25361):    at com.mat.archery.statistics.MySurfaceView.onDraw(MySurfaceView.java:130)
01-06 13:57:13.656: E/AndroidRuntime(25361):    at com.mat.archery.statistics.MySurfaceThread.run(MySurfaceThread.java:30)

为了更多地理解我的代码,这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test"
        android:textAppearance="?android:attr/textAppearanceLarge" />

<FrameLayout
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
<com.mat.archery.statistics.MySurfaceView
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"/>
</FrameLayout>
</LinearLayout>

这是我调用线程的.java:

public class MySurfaceThread extends Thread {
 private SurfaceHolder myThreadSurfaceHolder;
 private MySurfaceView myThreadSurfaceView;
 private boolean myThreadRun = false;

 public MySurfaceThread(SurfaceHolder surfaceHolder, MySurfaceView surfaceView) {
  myThreadSurfaceHolder = surfaceHolder;
  myThreadSurfaceView = surfaceView;
 }

 public void setRunning(boolean b) {
  myThreadRun = b;
 }

 @Override
 public void run() {
  // TODO Auto-generated method stub
  while(myThreadRun){
   Canvas c = null;

   try{
    c = myThreadSurfaceHolder.lockCanvas(null);
    synchronized (myThreadSurfaceHolder){
     myThreadSurfaceView.onDraw(c);
    }
    /*sleep(100);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();*/
   }
   finally{
    // do this in a finally so that if an exception is thrown
    // during the above, we don't leave the Surface in an
    // inconsistent state
    if (c != null) {
     myThreadSurfaceHolder.unlockCanvasAndPost(c);
    }
   }
  }
 }
}

这是我做Draw​​的.java,我在draw中调用receiveMyMessage(),因为它与draw密切相关。之前的计算不能按我的意愿工作,但这不是现在的问题:

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback{

 private MySurfaceThread thread;
 private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
 private Paint paintJaune = new Paint(Paint.ANTI_ALIAS_FLAG);
 int cx, cy, offx, offy;
 private float initX, initY, radius;
 private double distx, disty, distance;
 private boolean drawing = true;
 private boolean first = true;



public TargetActivity mActivity = null;


 public MySurfaceView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  init();
 }

 public MySurfaceView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
  init();
 }

 public MySurfaceView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  // TODO Auto-generated constructor stub
  init();
 }

  private void init(){
        Log.d("Essai", "TargetActivity  5");

        mActivity = new TargetActivity();

        getHolder().addCallback(this);
        thread = new MySurfaceThread(getHolder(), this);

        setFocusable(true); // make sure we get key events

        paint.setColor(getResources().getColor(R.color.Fleche1Default));
        paint.setStyle(Style.FILL);

        paintJaune.setColor(getResources().getColor(R.color.Jaune));
        paintJaune.setStyle(Style.FILL);



   }

 @Override
 public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
  // TODO Auto-generated method stub
  drawing = true;
 }

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  // TODO Auto-generated method stub
  thread.setRunning(true);
  thread.start();

 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
  // TODO Auto-generated method stub
  boolean retry = true;
  thread.setRunning(false);
  while (retry) {
   try {
    thread.join();
    retry = false;
   }
   catch (InterruptedException e) {
   }
  }
 }

 @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        //super.onDraw(canvas);
     int width = this.getWidth(); 
     int height = this.getHeight(); 



        if(drawing){
            canvas.drawColor(Color.BLACK);
            canvas.drawCircle(width/2, height/2, 80, paintJaune);
            canvas.drawCircle(initX, initY, radius, paint);

            distx = (width/2)-initX;
            distx *= distx;

            disty = (height/2)-initY;
            disty *= disty;

            distance = distx + disty;
            distance = Math.sqrt(distance);



            if((distance + radius/2) < 40)
            {   
                mActivity.receiveMyMessage();
            }

        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        //return super.onTouchEvent(event);

        int action = event.getAction();
        if (action==MotionEvent.ACTION_MOVE){
            initX = event.getX();
            initY = event.getY();
            radius = 30;
            drawing = true;
            first = false;
        }
        else if (action==MotionEvent.ACTION_DOWN){
            initX = event.getX();
            initY = event.getY();
            radius = 30;
            drawing = true;
            first = false;
        }
        else if (action==MotionEvent.ACTION_UP){
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {}
            drawing = false;
            first = false;
        }

        return true;

    }

       /*public void test() {

           mActivity.handler.sendEmptyMessage(0);
       }*/

}

@PratikSharma 编辑:更改后有这个 LogCat :

01-08 14:55:34.859: D/AndroidRuntime(22436): Shutting down VM
01-08 14:55:34.859: W/dalvikvm(22436): threadid=1: thread exiting with uncaught exception (group=0x4001f888)
01-08 14:55:34.882: E/AndroidRuntime(22436): FATAL EXCEPTION: main
01-08 14:55:34.882: E/AndroidRuntime(22436): java.lang.NullPointerException
01-08 14:55:34.882: E/AndroidRuntime(22436):    at android.app.Activity.findViewById(Activity.java:1637)
01-08 14:55:34.882: E/AndroidRuntime(22436):    at com.mat.archery.statistics.TargetActivity$1.run(TargetActivity.java:78)
01-08 14:55:34.882: E/AndroidRuntime(22436):    at android.os.Handler.handleCallback(Handler.java:587)
01-08 14:55:34.882: E/AndroidRuntime(22436):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-08 14:55:34.882: E/AndroidRuntime(22436):    at android.os.Looper.loop(Looper.java:123)
01-08 14:55:34.882: E/AndroidRuntime(22436):    at android.app.ActivityThread.main(ActivityThread.java:4668)
01-08 14:55:34.882: E/AndroidRuntime(22436):    at java.lang.reflect.Method.invokeNative(Native Method)
01-08 14:55:34.882: E/AndroidRuntime(22436):    at java.lang.reflect.Method.invoke(Method.java:521)
01-08 14:55:34.882: E/AndroidRuntime(22436):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
01-08 14:55:34.882: E/AndroidRuntime(22436):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
01-08 14:55:34.882: E/AndroidRuntime(22436):    at dalvik.system.NativeStart.main(Native Method)

第 78 行是:Text = (TextView) findViewById(R.id.textView1);

4

2 回答 2

2

试试这个:

public class TargetActivity extends Activity {

  TextView Text = null;
  private Handler mHandler = null;

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

       if(Text == null)
           Text = (TextView) findViewById(R.id.textView1); 
       if(mHandler == null)   
           mHandler = new Handler();
   }

编辑:

public void receiveMyMessage() {
        if(mHandler == null)
           mHandler = new Handler();
        if(Text == null)
           Text = (TextView) findViewById(R.id.textView1);

        mHandler.post(new Runnable() {
            @Override
            public void run() {                    
                Text.setText("Done");
            }
        });
    }
}

新编辑:

public void receiveMyMessage() {
        if(mHandler == null)
           mHandler = new Handler();
        new Thread() {
            public void run() {
                mHandler.post(mUpdateResults);
            }
    }.start();

}


final Runnable mUpdateResults = new Runnable() {
    public void run() {
        if(Text == null)
            Text = (TextView) findViewById(R.id.textView1);                    
        Text.setText("Done");
    }
}

谢谢。

于 2013-01-06T10:58:34.420 回答
0

添加文本不为空检查。我认为有时您在 onCreate 完成和 Text 初始化之前调用 receiveMyMessage 。

@Override
public void run() {
    if (Text != null) Text.setText("Done");
}
于 2013-01-06T11:12:41.257 回答