我正在编写一个用于可视化音频信号的 android 程序。主要 Activity 是:

package com.example.soundvisualizer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
private CDrawer.CDrawThread mDrawThread;
private CDrawer mdrawer;
private Button btn;
private View.OnClickListener listener;
private Boolean m_bStart;
private Boolean recording;
private CSampler sampler;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    m_bStart = false;
    mdrawer = (CDrawer) findViewById(R.id.drawer);
    while (true){
        recording = false;
        run();
        return;
    }
}

/**
 * Pause the visualizer when the app is paused
 */
@Override
protected void onPause(){
    System.out.println("onPause");
    sampler.SetRun(false);
    mDrawThread.setRun(false);
    sampler.SetSleeping(true);
    mDrawThread.SetSleeping(true);
    super.onPause();
}
/**
 * Resters the visualizer when the app restarts
 */
@Override
protected void onRestart(){
    m_bStart = true;
    System.out.println("onRestart");
    super.onRestart();
}
/**
 * Resume the visualizer when the app resumes
 */
@Override
protected void onResume(){
    System.out.println("onResume");
    int i = 0;
    while (true){
        if ((sampler.GetDead2()) && (mdrawer.GetDead2())){
            System.out.println(sampler.GetDead2() + ", " + mdrawer.GetDead2());
            sampler.Restart();
            if (!m_bStart.booleanValue())
                mdrawer.Restart(true);
            sampler.SetSleeping(false);
            mDrawThread.SetSleeping(false);
            m_bStart = false;
            super.onResume();
            return;
        }
        try{
            Thread.sleep(500L);
            System.out.println("Hang on..");
            i++;
            if (!sampler.GetDead2().booleanValue())
                System.out.println("sampler not DEAD!!!");
            if (!mdrawer.GetDead2().booleanValue()){
                System.out.println("mDrawer not DeAD!!");
                mdrawer.SetRun(false);
            }
            if (i <= 4)
                continue;
            mDrawThread.SetDead2(true);
        }
        catch (InterruptedException localInterruptedException){
            localInterruptedException.printStackTrace();
        }
    }
}

@Override
protected void onStart(){
    System.out.println("onStart");
    super.onStart();
}

@Override
protected void onStop(){
    System.out.println("onStop");
    super.onStop();
}


/**
 * Recives the buffert from the sampler
 * @param buffert
 */
public void setBuffer(short[] paramArrayOfShort){
    mDrawThread = mdrawer.getThread();
    mDrawThread.setBuffer(paramArrayOfShort);
}

/**
 * Called by OnCreate to get everything up and running
 */
public void run(){
    try {
        if (mDrawThread == null){
            mDrawThread = mdrawer.getThread();
        }
        if (sampler == null)
            sampler = new CSampler(this);
        Context localContext = getApplicationContext();
        Display localDisplay = getWindowManager().getDefaultDisplay();
        Toast localToast = Toast.makeText(localContext, "Please make some noise..", Toast.LENGTH_LONG);
        localToast.show();
        if (sampler != null){
            try {
                sampler.Init();
            }
            catch (Exception e) {
                Toast.makeText(this,"Could not instance the sampler. Could not access the device audio-drivers", Toast.LENGTH_LONG).show();
            }
            btn = (Button) this.findViewById(R.id.button_stop);
            btn.setOnClickListener(this);
            sampler.StartRecording();
            sampler.StartSampling();
        }
      }
    catch (NullPointerException e) {
        Log.e("Main_Run", "NullPointer: " + e.getMessage());
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(btn.getText().equals("Start")){
        btn.setText("Stop");
        sampler.StartRecording();
        sampler.StartSampling();
    }
    else{
        btn.setText("Start");
        sampler.StopRecording();
    }
}

}

现在,该应用程序启动并运行良好。我点击停止按钮就OK了。重新单击开始,重启工作正常。当我再次单击停止时,应用程序崩溃并且 logcat 显示以下行来保护我的应用程序:
I/ActivityManager(  755): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.soundvisualizer/.MainActivity} from pid 1054
I/ActivityManager(  755): Start proc com.example.soundvisualizer for activity com.example.soundvisualizer/.MainActivity: pid=6959 uid=10126 gids={50126}
I/ActivityManager(  755): Displayed com.example.soundvisualizer/.MainActivity: +710ms
W/ActivityManager(  755):   Force finishing activity com.example.soundvisualizer/.MainActivity
W/ActivityManager(  755): Exception thrown during pause
W/ActivityManager(  755): android.os.DeadObjectException
W/ActivityManager(  755):   at android.os.BinderProxy.transact(Native Method)
W/ActivityManager(  755):   at android.app.ApplicationThreadProxy.schedulePauseActivity(ApplicationThreadNative.java:660)
W/ActivityManager(  755):   at com.android.server.am.ActivityStack.startPausingLocked(ActivityStack.java:761)
W/ActivityManager(  755):   at com.android.server.am.ActivityStack.finishActivityLocked(ActivityStack.java:2455)
W/ActivityManager(  755):   at com.android.server.am.ActivityStack.finishTopRunningActivityLocked(ActivityStack.java:2329)
W/ActivityManager(  755):   at com.android.server.am.ActivityStackSupervisor.finishTopRunningActivityLocked(ActivityStackSupervisor.java:2035)
W/ActivityManager(  755):   at com.android.server.am.ActivityManagerService.handleAppCrashLocked(ActivityManagerService.java:9543)
W/ActivityManager(  755):   at com.android.server.am.ActivityManagerService.makeAppCrashingLocked(ActivityManagerService.java:9436)
W/ActivityManager(  755):   at com.android.server.am.ActivityManagerService.crashApplication(ActivityManagerService.java:10081)
W/ActivityManager(  755):   at com.android.server.am.ActivityManagerService.handleApplicationCrashInner(ActivityManagerService.java:9632)
W/ActivityManager(  755):   at com.android.server.am.NativeCrashListener$NativeCrashReporter.run(NativeCrashListener.java:86)
I/ActivityManager(  755): Config changes=480 {1.0 222mcc10mnc it_IT ldltr sw360dp w360dp h567dp 480dpi nrml port finger -keyb/v/h -nav/h s.583}
I/ActivityManager(  755): Process com.example.soundvisualizer (pid 6959) has died.
W/ActivityManager(  755): No content provider found for permission revoke: file:///data/local/tmp/SoundVisualizer.apk
W/ActivityManager(  755): No content provider found for permission revoke: file:///data/local/tmp/SoundVisualizer.apk
I/ActivityManager(  755): Force stopping com.example.soundvisualizer appid=10126 user=-1: uninstall pkg
I/ActivityManager(  755): Force stopping com.example.soundvisualizer appid=10126 user=-1: update pkg
I/ActivityManager(  755): Force stopping com.example.soundvisualizer appid=10126 user=0: pkg removed

我不知道导致错误的原因以及如何修复它。谁能帮我?
提前致谢。

编辑
我找到了 this link,其中解释了 sigabrt 是什么类型的错误。该表表示可以使用 -Xrs 禁用 sigabrt。如何在 Eclipse IDE 中使用 -Xrs?

最佳答案

在这种情况下发生在我身上:

  • 我在创建时加载一个 View ,并且在一个函数被非常快地调用并访问该 View 之后。

  • View 可以在某个瞬间为空!!!

    在您的情况下,每次访问该 View 时都将其输入:
    if(mdrawer!=null){
       //DO Whatever
    }else{
       Log.e(TAG,"ANDROID BUG");
    }
    

    编辑:我意识到这不是因为函数调用速度非常快。这是因为我有一个 Listener 并且我没有在 OnDestroy() 时取消注册。当 OnCreate 时,我有两个监听器。一个结果为 NULL,另一个正常。
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mSensorManager!=null){mSensorManager.unregisterListener(listener);}
    }
    

    关于android - 致命信号 6 SIGABRT 代码=-6,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22577105/

    10-13 05:00