所以我有这段代码可以在其他线程中添加一个动作时更改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);
}
}
}
}
}
这是我执行绘图的.java文件,我在绘图中称为receiveMyMessage(),因为它与绘图紧密相关。计算之前不起作用,但现在这不是问题:
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);
最佳答案
试试这个:
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");
}
}
谢谢。