我在此行收到此错误 按钮button1 =(按钮)findViewById(R.id.alarm);
java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.user.appidea / com.example.user.appidea.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'android.view。空对象引用上的Window $ Callback android.view.Window.getCallback()'
public class MainActivity extends AppCompatActivity {
private Button button;
**Button button1 = (Button) findViewById(R.id.alarm);**
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
INSERT CODE HERE
}
});
}
}
答案 0 :(得分:0)
尝试这个工作示例
final MediaPlayer mpRingtone = MediaPlayer.create(this, R.raw.musicfile);
mpRingtone.start();