我尝试做这样的意图:
public class CenterAnalyse extends Activity {
public final static String AGE = "com.mat.archery.statistics.AGE";
protected MySurfaceView mActivity;
public TextView Text;
public void receiveMyMessage()
{
Log.d("Essai", "Test 1");
Intent intent = new Intent(CenterAnalyse.this, TargetActivity.class);
Log.d("Essai", "Test 2");
intent.putExtra(AGE, 1);
Log.d("Essai", "Test 3");
// Puis on lance l'intent !
startActivity(intent);
}
}
我调用test()
这段代码:
public class TargetActivity extends Activity {
protected MySurfaceView mActivity;
public TextView Text;
/** 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);
Intent i = getIntent();
int age = i.getIntExtra(CenterAnalyse.AGE, 0);
if(age == 1)
{
test();
age = 0;
}
}
private void test()
{
Log.d("Essai", "Test 5");
runOnUiThread(new Runnable() {
@Override
public void run() {
if (Text != null) Text.setText("Done");
}
});
}
}
但是当receiveMyMessage()
在另一个线程中调用时,我得到了这些错误:
01-08 09:04:41.920: D/Essai(12895): Test 1
01-08 09:04:41.935: W/dalvikvm(12895): threadid=9: thread exiting with uncaught exception (group=0x4001f888)
01-08 09:04:42.021: E/AndroidRuntime(12895): FATAL EXCEPTION: Thread-9
01-08 09:04:42.021: E/AndroidRuntime(12895): java.lang.NullPointerException
01-08 09:04:42.021: E/AndroidRuntime(12895): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
01-08 09:04:42.021: E/AndroidRuntime(12895): at android.content.ComponentName.<init>(ComponentName.java:75)
01-08 09:04:42.021: E/AndroidRuntime(12895): at android.content.Intent.<init>(Intent.java:2900)
01-08 09:04:42.021: E/AndroidRuntime(12895): at com.mat.archery.statistics.CenterAnalyse.receiveMyMessage(CenterAnalyse.java:26)
01-08 09:04:42.021: E/AndroidRuntime(12895): at com.mat.archery.statistics.MySurfaceView.onDraw(MySurfaceView.java:137)
01-08 09:04:42.021: E/AndroidRuntime(12895): at com.mat.archery.statistics.MySurfaceThread.run(MySurfaceThread.java:43)
我知道我做错了什么,我只是 java/android 的初学者,所以有人可以告诉我我在意图或其他地方做错了什么?
编辑:这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 CenterAnalyse 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 CenterAnalyse();
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);
//Log.d("Essai", String.valueOf(distance));
if((distance + radius/2) < 110)
{
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;
}
}
这就是抽奖的地方:
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 上的测试中更改 textview,我接受它!