几次点击按钮后,Android应用程序崩溃了

时间:2016-06-14 15:24:06

标签: android

任何人都可以帮助解决以下问题:

我创建了一个包含2个按钮的简单活动,每次我开始在按钮上随机点击(即使慢点击),当总点击次数达到30次点击时,应用会崩溃。

爪哇:

import android.media.AudioManager;
import android.media.ToneGenerator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final View button_1=(findViewById(R.id.button_1));
        button_1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                AudioManager am = (AudioManager) getSystemService(MainActivity.AUDIO_SERVICE);
                int volume_level= am.getStreamVolume(AudioManager.STREAM_RING); 
                final ToneGenerator mToneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, volume_level * 14);
                mToneGenerator.stopTone();
                mToneGenerator.startTone(ToneGenerator.TONE_DTMF_1, 100);
            }
        });

        final View button_2=(findViewById(R.id.button_2));
        button_2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                AudioManager am = (AudioManager) getSystemService(MainActivity.AUDIO_SERVICE);
                int volume_level= am.getStreamVolume(AudioManager.STREAM_RING); 
                final ToneGenerator mToneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, volume_level * 14);
                mToneGenerator.stopTone();
                mToneGenerator.startTone(ToneGenerator.TONE_DTMF_9, 100);
            }
        });
    }


}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button_1"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button_2"
        android:layout_below="@+id/button_1"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

0 个答案:

没有答案