nullpointerexception线程主要由于错误而退出

时间:2012-04-09 18:31:22

标签: android eclipse nullpointerexception

错误是:

 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

1 个答案:

答案 0 :(得分:1)

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

将这些变量初始化为onCreate并且该问题应该消失