0

那么错误是:

 W/dalvikvm(320): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
 E/AndroidRuntime(320): Uncaught handler: thread main exiting due to uncaught exception
 E/AndroidRuntime(320): java.lang.NullPointerException
 E/AndroidRuntime(320):     at com.android.vosori.Amp$1.onCheckedChanged(Amp.java:58)
 E/AndroidRuntime(320):     at android.widget.CompoundButton.setChecked(CompoundButton.java:122)
E/AndroidRuntime(320):  at android.widget.ToggleButton.setChecked(ToggleButton.java:66)
 E/AndroidRuntime(320):     at android.widget.CompoundButton.toggle(CompoundButton.java:85)
 E/AndroidRuntime(320):     at android.widget.CompoundButton.performClick(CompoundButton.java:97)
 E/AndroidRuntime(320):     at android.view.View.onTouchEvent(View.java:4179)
 E/AndroidRuntime(320):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
 E/AndroidRuntime(320):     at android.view.View.dispatchTouchEvent(View.java:3709)      E/AndroidRuntime(320):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
 E/AndroidRuntime(320):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
 E/AndroidRuntime(320):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
 E/AndroidRuntime(320):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
E/AndroidRuntime(320):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
E/AndroidRuntime(320):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
 E/AndroidRuntime(320):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
 E/AndroidRuntime(320):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
 E/AndroidRuntime(320):     at android.os.Handler.dispatchMessage(Handler.java:99)
 E/AndroidRuntime(320):     at android.os.Looper.loop(Looper.java:123)
 E/AndroidRuntime(320):     at android.app.ActivityThread.main(ActivityThread.java:4363)
 E/AndroidRuntime(320):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(320):  at java.lang.reflect.Method.invoke(Method.java:521)
 E/AndroidRuntime(320):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
 E/AndroidRuntime(320):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
 E/AndroidRuntime(320):     at dalvik.system.NativeStart.main(Native Method)

和..来源是:

public class Amp extends Activity implements OnSeekBarChangeListener {

    WakeLock wL;
    public boolean tb;
    SeekBar volume;
    AudioManager am;
    MediaPlayer mp;
    AudioRecord arec;
    AudioTrack atrack;
    ToggleButton tbs;
    static final int buffersize = 200000;
    final short[] buffer = new short[buffersize];
    short[] readBuffer = new short[buffersize];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        // wake-lock
        PowerManager pM = (PowerManager) getSystemService(Context.POWER_SERVICE);
        WakeLock wL = pM.newWakeLock(PowerManager.FULL_WAKE_LOCK, "whatever");

        super.onCreate(savedInstanceState);
        wL.acquire();
        // fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
            android.os.Process
                    .setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
            int bufferSize = AudioRecord.getMinBufferSize(8000,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT);

            arec = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, bufferSize);
            atrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, 8000,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO,
                    AudioFormat.ENCODING_PCM_16BIT, bufferSize,
                    AudioTrack.MODE_STREAM);
            atrack.setPlaybackRate(8000);
            byte[] buffer = new byte[bufferSize];


            while (tb) {
                arec.read(buffer, 0, bufferSize);
                atrack.write(readBuffer, 0, buffer.length);
            }

        tbs = (ToggleButton) findViewById(R.id.tbS);
        tbs.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    arec.startRecording();
                    atrack.play();
                } else {
                    arec.stop();
                    atrack.stop();

                }

            }
        });

        volume = (SeekBar) findViewById(R.id.sb1);
        am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        int maxV = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int curV = am.getStreamVolume(AudioManager.STREAM_MUSIC);
        volume.setMax(maxV);
        volume.setProgress(curV);
        volume.setOnSeekBarChangeListener(this);
    }

    public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
        // TODO Auto-generated method stub
        am.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
        am.setSpeakerphoneOn(true);

    }

    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }

    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }


}

你能帮我么?我被困在这几天,找不到解决这个问题的方法

更新编辑:抱歉 D:我对这个不太好 D:我可能在这里犯了一个错误,也尝试将这些东西移到 oncheckedchanged 方法中,但没有用:S

4

1 回答 1

1

在调用方法“run”之前,您不会初始化 arec 和 atrack 变量。我在您的代码中看不到您正在调用方法 run() 的任何地方,因此这些变量将永远不会被初始化。

将这些变量的初始化移到 onCreate 中,这个问题应该会消失

于 2012-04-09T18:41:21.847 回答