0

我尝试做这样的意图:

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,我接受它!

4

6 回答 6

2

使用它来检索意图的数据:

 Bundle bundle = this.getIntent().getExtras();
 if (bundle != null && bundle.containsKey(AGE)) {
     int age = bundle.getInt(AGE);
 }
于 2013-01-08T08:22:22.263 回答
0

这可能是由于您没有TargetActivity在清单文件中提及您的课程。请检查一下

并使用

Intent intent = new Intent(CenterAnalyse.this, TargetActivity.class);

这样它将获得适当的活动上下文

通过看到您的问题,我只能说您必须调用intentUI thread必须调用 as 的原件contextactivity因为您正在调用intentthread提供 that 的实例而thread不是contextofactivity

于 2013-01-08T08:33:46.967 回答
0

我认为您忘记protected MySurfaceView mActivity;在应用程序中初始化 。只需将您的MySurfaceView活动初始化为:

  protected MySurfaceView mActivity;
    @Override
       public void onCreate(Bundle savedInstanceState) {    
       super.onCreate(savedInstanceState);  
      ......................
      mActivity=new MySurfaceView();
   }
于 2013-01-08T08:30:32.853 回答
0
mActivity = new CenterAnalyse();

不能用作启动活动的上下文,因为它没有被系统正确初始化。您不能自己实例化活动,它需要由 android 完成,因此上下文附加到应用程序。

您需要使用getContext().startActivity()SurfaceView 来启动活动。

于 2013-01-08T09:20:20.307 回答
0

只需使用,

 Intent intent = new Intent(CenterAnalyse.this, TargetActivity.class);
    intent.putExtra("AGE", 1);

在第二类使用中,

      Intent i = getIntent();
       int age = i.getIntExtra("AGE", 0);

行不通。

编辑-您是否初始化并创建了您的活动。?

于 2013-01-08T08:21:51.353 回答
0

Intent intent = new Intent(this, TargetActivity.class);

也许改成

Intent intent = new Intent(CenterAnalyse.this, TargetActivity.class);

于 2013-01-08T08:25:15.053 回答