我有一个关于Handler中TextView的java.lang.NullPointerException

时间:2013-01-06 10:53:22

标签: android multithreading nullpointerexception textview handler

所以当有一个动作附加在另一个帖子中时,我有这个代码来改变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,我在绘图中调用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);

2 个答案:

答案 0 :(得分: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");
    }
}

感谢。

答案 1 :(得分:0)

添加文字非空检查。我想有时你会在onCreate完成并初始化Text之前调用receiveMyMessage。

@Override
public void run() {
    if (Text != null) Text.setText("Done");
}