我正在尝试使用Android按钮“Play”构建一个简单的应用程序。在MainActivty.java文件中,我收到以下代码的错误,所以我需要在这里纠正什么?
package com.example.simplemusicplayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Button playButton=(Button) findViewById(R.id.button1);
playButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//TODO Auto generated method stub
Toast.makeText(MainActivity.this,"Starting to play",Toast.LENGTH_SHORT).show();
}
});
}
}
答案 0 :(得分:1)
移动
Button playButton=(Button) findViewById(R.id.button1);
playButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//TODO Auto generated method stub
Toast.makeText(MainActivity.this,"Starting to play",Toast.LENGTH_SHORT).show();
}
});
在onCreate()
内。
从}
的末尾删除额外的onCreateOptionsMenu
。
我还建议你阅读Building Your First Android App,这是一本很好的指南,可以教你基础知识。
答案 1 :(得分:0)
试试这段代码
package com.example.simplemusicplayer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button playButton = (Button) findViewById(R.id.button1);
playButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto generated method stub
Toast.makeText(MainActivity.this, "Starting to play",
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}