Android runOnUiThread中TextView上的NullPointerException

Android runOnUiThread中TextView上的NullPointerException,android,multithreading,nullpointerexception,Android,Multithreading,Nullpointerexception,所以我有以下代码: 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) {

所以我有以下代码:

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中的文本,但在调用时我遇到以下错误:

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)
我不明白为什么它是空指针,如果我对行进行注释,则没有错误,但TextView没有更改,因此文本为空

如果有帮助,以下是布局:

<?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>
这就是抽签的地方,在这里,receiveMyMessage是call:

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;

    }

}
公共类MySurfaceView扩展了SurfaceView,实现了SurfaceHolder.Callback{
私有MySurfaceThread线程;
私有油漆=新油漆(油漆.防油漆别名标志);
私有油漆paintJaune=新油漆(油漆.防油漆别名标志);
内勤cx、cy、offx、offy;
私有浮点initX、initY、radius;
公共双距离,距离,距离=100;
私有布尔图形=真;
private boolean first=true;
公共布尔触摸=假;
公共文本查看文本;
受保护的MySurfaceThread msurfacethread;
公共目标活动mActivity=null;
公共MySurfaceView(上下文){
超级(上下文);
//TODO自动生成的构造函数存根
init();
}
公共MySurfaceView(上下文、属性集属性){
超级(上下文,attrs);
//TODO自动生成的构造函数存根
init();
}
公共MySurfaceView(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
//TODO自动生成的构造函数存根
init();
}
私有void init(){
Log.d(“Essai”、“目标活动5”);
getHolder().addCallback(此);
thread=newmysurfacethread(getHolder(),this);
setFocusable(true);//确保获取关键事件
setColor(getResources().getColor(R.color.Fleche1Default));
油漆.设置样式(样式.填充);
setColor(getResources().getColor(R.color.Jaune));
paintJaune.setStyle(Style.FILL);
mActivity=新的TargetActivity();
Text=(TextView)findViewById(R.id.textView1);
}
@凌驾
公共无效表面更改(表面更改arg0、int arg1、int arg2、int arg3){
//TODO自动生成的方法存根
绘图=真;
}
@凌驾
已创建的公共空白表面(表面持有人){
//TODO自动生成的方法存根
thread.setRunning(true);
thread.start();
}
@凌驾
公共空间表面覆盖(表面覆盖物持有人){
//TODO自动生成的方法存根
布尔重试=真;
thread.setRunning(false);
while(重试){
试一试{
thread.join();
重试=错误;
}
捕捉(中断异常e){
}
}
}
@凌驾
受保护的void onDraw(画布){
//TODO自动生成的方法存根
//super.onDraw(帆布);
int width=this.getWidth();
int height=this.getHeight();
if(图纸){
画布。drawColor(颜色。黑色);
画布.画圈(宽/2,高/2,80,画圈);
画布.画圈(初始、初始、半径、绘制);
if(touch==true){
distx=(宽度/2)-initX;
distx*=distx;
距离=(高度/2)-距离;
disty*=disty;
距离=distx+disty;
距离=数学sqrt(距离);
}
如果((距离+半径/2)<40)
{   
mActivity.receiveMyMessage();
}
}
}
@凌驾
公共布尔onTouchEvent(运动事件){
//TODO自动生成的方法存根
//返回super.onTouchEvent(事件);
int action=event.getAction();
if(action==MotionEvent.action\u MOVE){
initX=event.getX();
initY=event.getY();
半径=30;
绘图=真;
第一个=假;
触摸=真实;
}
else if(action==MotionEvent.action\u DOWN){
initX=event.getX();
initY=event.getY();
半径=30;
绘图=真;
第一个=假;
触摸=真实;
}
else if(action==MotionEvent.action\u UP){
试一试{
睡眠(50);
}捕获(中断异常e){}
图纸=假;
第一个=假;
触摸=假;
}
返回true;
}
}

这就是让你绊倒的原因

mActivity=newtargetactivity()

你不能那样做。只有系统可以实例化活动

你也许能逃脱<代码>mActivity=(TargetActivity)getContext()

我不想再提了,但它会解决你眼前的问题

您应该考虑使用广播意图发送消息。

getContext().sendBroadcast(新意图(“绘图完成”)


在这种情况下,您必须实现自己的广播接收器

在onCreate中找到TextView之后,为什么还要再次尝试查找它?这可能与调用
活动的显式构造函数有关。无论如何,
findViewById()
不应该抛出npe,但它可以返回null,堆栈跟踪是否还说了什么?何时调用
receiveMyMessage()
findViewById
中唯一的
null
是如果
getWindow()
为null。我认为A--C是对的;几乎可以肯定的是,显式调用
活动的构造函数是导致问题的原因。绘图(代码的第三部分)中调用了
receiveMyMessage()
,我可以用什么来替换
Acivity
?(我是java/android的初学者)据我所知,如果需要更新
TargetActivity
中的UI,则通过
Intent
发送数据,并创建
活动。将该活动中的
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;

    }

}