所以我有这段代码:
public class TargetActivity extends Activity {
protected MySurfaceView mActivity;
public TextView Text = null;
/** 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);
}
public void receiveMyMessage()
{
Log.d("Essai", "Test 1");
Text = (TextView) findViewById(R.id.textView1);
Log.d("Essai", "Test 2");
runOnUiThread(new Runnable() {
@Override
public void run() {
if (Text != null) Text.setText("Done");
}
});
}
}
我在一个线程中调用receiveMyMessage来更改TextView中的文本,但是当调用il时我发生了这个错误:
01-07 22:13:01.375: D/Essai(31552): Test 1
01-07 22:13:01.406: W/dalvikvm(31552): threadid=9: thread exiting with uncaught exception (group=0x4001f888)
01-07 22:13:01.406: E/AndroidRuntime(31552): FATAL EXCEPTION: Thread-9
01-07 22:13:01.406: E/AndroidRuntime(31552): java.lang.NullPointerException
01-07 22:13:01.406: E/AndroidRuntime(31552): at android.app.Activity.findViewById(Activity.java:1637)
01-07 22:13:01.406: E/AndroidRuntime(31552): at com.mat.archery.statistics.TargetActivity.receiveMyMessage(TargetActivity.java:36)
01-07 22:13:01.406: E/AndroidRuntime(31552): at com.mat.archery.statistics.MySurfaceView.onDraw(MySurfaceView.java:133)
01-07 22:13:01.406: E/AndroidRuntime(31552): at com.mat.archery.statistics.MySurfaceThread.run(MySurfaceThread.java:43)
第36行是:Text = (TextView) findViewById(R.id.textView1);
我不明白为什么它是一个NullPointer,如果我评论该行没有错误,但TextView没有改变,所以Text为null。
如果可以提供帮助,这就是布局:
<?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>
这是我调用另一个帖子的地方:
public class MySurfaceThread extends Thread {
private SurfaceHolder myThreadSurfaceHolder;
private MySurfaceView myThreadSurfaceView;
private boolean myThreadRun = false;
public double distance;
public String result;
public boolean touch = false;
public MySurfaceView mSurfaceView = null;
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);
}
}
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,其中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;
public double distx, disty, distance = 100;
private boolean drawing = true;
private boolean first = true;
public boolean touch = false;
public TextView Text;
protected MySurfaceThread msurfacethread;
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");
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);
mActivity = new TargetActivity();
Text = (TextView) findViewById(R.id.textView1);
}
@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);
if (touch == true){
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;
touch = true;
}
else if (action==MotionEvent.ACTION_DOWN){
initX = event.getX();
initY = event.getY();
radius = 30;
drawing = true;
first = false;
touch = true;
}
else if (action==MotionEvent.ACTION_UP){
try {
Thread.sleep(50);
} catch (InterruptedException e) {}
drawing = false;
first = false;
touch = false;
}
return true;
}
}
答案 0 :(得分:0)
这就是绊倒你
mActivity = new TargetActivity();
你不能这样做。只有系统才能实例化活动。
你或许能够离开。 mActivity = (TargetActivity) getContext();
我不会推荐这个,但它可以解决你的直接问题。
您应该考虑使用广播意图来发送消息。
getContext().sendBroadcast(new Intent("DRAWING_FINISHED"));
在这种情况下,您必须实现自己的广播接收器。