2012-02-29 24 views
0

メニューが表示され、他のプログラムに表示されるかどうかを確認するために独立したコードを作成しようとしましたが、このコードでは表示されません。
mPaint =新しいペイント(:(のMenuItemアイテム){ スイッチ(item.getItemId()){ 場合R.id.eraser onOptionsItemSelectedメニューオプションが表示されない理由

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    dp = new Panel(this); 
    dp.setOnTouchListener(this); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    mPaint = new Paint(); 
    mPaint.setDither(true); 
    mPaint.setColor(Color.BLACK); 
    mPaint.setStyle(Paint.Style.STROKE); 
    mPaint.setStrokeJoin(Paint.Join.MITER); 
    mPaint.setStrokeCap(Paint.Cap.BUTT); 
    mPaint.setStrokeWidth(5); 

    //set framelayout for the head layout 
    fl = new FrameLayout(this); 
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
      LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 
      WindowManager.LayoutParams.TYPE_APPLICATION, 
      WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, 
      PixelFormat.TRANSLUCENT); 
    fl.addView(dp,LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); 
    this.getWindowManager().addView(fl , lp); 

    //cancel button 
    Button cancel = new Button(this); 
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, 75); 
    params.bottomMargin =LayoutParams.MATCH_PARENT ; 
    params.gravity =Gravity.BOTTOM; 
    params.leftMargin =LayoutParams.MATCH_PARENT; 
    params.gravity =Gravity.BOTTOM; 
    cancel.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)); 
    fl.addView(cancel, params); 
    cancel.setOnClickListener(new ButtonClickHandlerCancel()); 

    //save button 
    Button save = new Button(this); 
    FrameLayout.LayoutParams params2 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, 75); 
    params2.topMargin = LayoutParams.MATCH_PARENT; 
    params2.gravity =Gravity.BOTTOM; 
    params2.rightMargin = LayoutParams.MATCH_PARENT; 
    params2.bottomMargin = 75; 
    params2.gravity =Gravity.BOTTOM; 
    save.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)); 
    fl.addView(save, params2); 
    save.setOnClickListener(new ButtonClickHandlerSave()); 

    //aesthetics for cancel button 
    StateListDrawable states = new StateListDrawable(); 
    states.addState(new int[] {android.R.attr.state_pressed}, 
     getResources().getDrawable(R.drawable.cpressed)); 
    states.addState(new int[] {android.R.attr.state_focused}, 
     getResources().getDrawable(R.drawable.cunpressed)); 
    states.addState(new int[] { }, 
     getResources().getDrawable(R.drawable.cpressed)); 
    cancel.setBackgroundDrawable(states); 

    //aesthetics for save button 
    StateListDrawable states2 = new StateListDrawable(); 
    states2.addState(new int[] {android.R.attr.state_pressed}, 
     getResources().getDrawable(R.drawable.spressed)); 
    states2.addState(new int[] {android.R.attr.state_focused}, 
     getResources().getDrawable(R.drawable.sunpressed)); 
    states2.addState(new int[] { }, 
     getResources().getDrawable(R.drawable.spressed)); 
    save.setBackgroundDrawable(states2); 
} 
@Override 
    public boolean onCreateOptionsMenu(Menu menu){ 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.layout.menu, menu); 
    return true; 

} @Overrideパブリックブール); mPaint.setDither(true); mPaint.setColor(Color.TRANSPARENT); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.BUTT); mPaint.setStrokeWidth(5); 休憩。 ケースR.id.paint: mPaint = new Paint(); mPaint.setDither(true); mPaint.setColor(Color.BLACK); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.MITER); mPaint.setStrokeCap(Paint.Cap.BUTT); mPaint.setStrokeWidth(5); 休憩。 } がtrueを返します。 }

//when cancel button is pressed 
public class ButtonClickHandlerCancel implements View.OnClickListener { 
    public void onClick(View view) { 
     Intent intent = new Intent(MaleLayout.this, 
       BillysCrownMain.class); 
     startActivity(intent); 
     MaleLayout.this.finish();   
    } 
} 

//when save button is pressed 
public class ButtonClickHandlerSave implements View.OnClickListener { 
    public void onClick(View view) { 
      dp.saveScreenshot(); 
        } 
    } 

//automatically created from panel 
    @Override 
    protected void onPause() { 
     super.onPause(); 
     dp.pause(); 
    } 
    //automatically created from panel 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     dp.resume(); 
    } 


//setting the path of paint based on finger touch 
@Override 
public boolean onTouch(View v, MotionEvent me) { 
      synchronized(pointsToDraw) 
      { 
    if(me.getAction() == MotionEvent.ACTION_DOWN){ 
     path = new Path(); 
     path.moveTo(me.getX(), me.getY()); 
     pointsToDraw.add(path); 
    }else if(me.getAction() == MotionEvent.ACTION_MOVE){ 
     path.lineTo(me.getX(), me.getY()); 
    }else if(me.getAction() == MotionEvent.ACTION_UP){ 
    } 
    }  
    return true; 
} 

//the panel where the paint will be created 
public class Panel extends SurfaceView implements Runnable{ 

    public Panel(Context context) { 
     super(context); 
     holder = getHolder(); 

    } 

    @Override 
    public void run() { 
     while(isItOk == true){ 

      if(!holder.getSurface().isValid()){ 
       continue; 
      } 
      holder.setFormat(PixelFormat.TRANSLUCENT); 
      c = holder.lockCanvas(); 
      onDraw(c); 
      holder.unlockCanvasAndPost(c); 

     } 
    } 

    //the canvas where paint is drawn and background set 
    @Override 
    protected void onDraw(Canvas canvas) { 
     Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.malehead); 
     Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, fl.getWidth(), fl.getHeight()-150, true); 
     c.drawBitmap(bMapScaled,0,0,null); 

     super.onDraw(canvas); 
     synchronized(pointsToDraw) 
     { 
     for (Path path : pointsToDraw) 
     { 
      canvas.drawPath(path, mPaint); 
     } 
     } 
    } 

    public void pause(){ 
     isItOk = false; 
     while(true){ 
      try{ 
       t.join(); 
      }catch(InterruptedException e){ 
       e.printStackTrace(); 
      } 
      break; 
     } 
     t = null; 
    } 

    public void resume(){ 
     isItOk = true; 
     t = new Thread(this); 
     t.start(); 

    } 


//saving the file 
public void saveScreenshot() { 
    if (ensureSDCardAccess()) { 
     Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.malehead); 
     Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, fl.getWidth(), fl.getHeight()-150, true); 
     Canvas canvas = new Canvas(bMapScaled); 
     onDraw(canvas); 
     File file = new File(mScreenshotPath + "/" + System.currentTimeMillis() + ".jpg"); 
     FileOutputStream fos; 
     try { 
      fos = new FileOutputStream(file); 
      Toast.makeText(MaleLayout.this, "Image saved at: " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show(); 
      bMapScaled.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
      fos.close(); 
     } catch (FileNotFoundException e) { 
      Log.e("Panel", "FileNotFoundException", e); 
     } catch (IOException e) { 
      Log.e("Panel", "IOEception", e); 
     } 
    } 
} 

private boolean ensureSDCardAccess() { 
    File file = new File(mScreenshotPath); 
    if (file.exists()) { 
     return true; 
    } else if (file.mkdirs()) { 
     return true; 
    } 
    return false; 
} 
} 

答えて

0

交換してみてください:

これにより
public boolean onCreateOptionsMenu(Menu menu){ 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.layout.menu, menu); 
    return true; 
} 

public boolean onCreateOptionsMenu(Menu menu){ 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.layout.menu, menu); 
    return(super.onCreateOptionsMenu(menu)); 
} 

また「あなたのmenu.xmlレイアウトが正しい場所にあることを確認するためにチェックして、そこに上がりませんその中に何らかの誤りがあります。あなたはあなたのアプリを実行するときにLogCatのエラーを見ますか?

関連する問題