0

我在这条线上得到这个错误按钮 button1 = (Button) 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

            }
        });
    }
}
4

1 回答 1

0

试试这个工作示例

final MediaPlayer mpRingtone = MediaPlayer.create(this, R.raw.musicfile);
mpRingtone.start();
于 2018-12-27T11:23:31.457 回答