我试图创建一个独立的代码来检查菜单是否会出现并且它出现在其他程序中但是在这段代码中,它没有出现,可能是什么问题?
@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;
} @覆盖 public boolean onOptionsItemSelected(MenuItem item){ 开关(item.getItemId()){ 案例R.id.eraser:
mPaint = new Paint(); mPaint.setDither(真); 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(真); 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 :(得分: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中看到任何错误?