1

だから私はこのコードを持っています:

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); です。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;

    }

}
4

1 に答える 1

0

これはあなたをつまずかせているものです

mActivity = new TargetActivity();

そんなことはできません。アクティビティをインスタンス化できるのはシステムだけです。

あなたは逃げることができるかもしれません。mActivity = (TargetActivity) getContext();

私はそれをお勧めしませんが、それはあなたの差し迫った問題を解決するでしょう.

ブロードキャスト インテントを使用してメッセージを送信することを検討する必要があります。

getContext().sendBroadcast(new Intent("DRAWING_FINISHED"));

その場合、独自の Broadcast Receiver を実装する必要があります。

于 2013-05-05T16:36:25.777 に答える