立即App崩溃,只要添加单行代码

时间:2012-06-19 09:44:10

标签: android

这是一个完美运行的类,直到我想再添加一个功能,代码编译然后在执行时立即崩溃。
问题在于我希望在这个类中实现的componentAnalyzer类。我不知道为什么它不起作用,因为我以完全相同的方式在另一个类中实现了这个componentAnalyzer并且它工作得非常好。

我认为这是一个小错误但不确定。我评论了创造问题的部分,因为其余的工作,不应该触及。

将使用componentAnalyzer的方法位于代码的末尾。我删除了所有正在工作的东西,以便更容易看到它。

public class PowerMonitorActivity extends Activity implements SeekBar.OnSeekBarChangeListener {
    private static boolean instantiated = false;

    //private static ComponentAnalyzer componentAnalyzer;
    //private Context context;

    private Button changeGPSButton;
    private Button changeAudioButton;       
    private Button locationUpdateButton;        
    private SeekBar brightnessSeekBar;
    private int screenBrightness = 123;         
    private Handler cpuHandler = new Handler();
    private CPUMonitor cpuMonitor = new CPUMonitor();
    private int updateTime = 1000;

    private Handler totalPowerHandler = new Handler ();         
    private MediaManager mediaManager = new MediaManager();
    private boolean requestingLocation = false;

    private boolean wifiIsTransmitting = false;
    private boolean wifiIsConnected = false;
    private boolean cellIsTransmitting = false;
    private boolean cellIsConnected = false;
    private boolean isLogging = false;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Bundle extras = getIntent().getExtras();
        if (!instantiated) {
            instantiated = true;
                 //componentAnalyzer = new ComponentAnalyzer(context, extras);
        }

        // Create GPS button - note that location settings cannot be changed directly
        // in a program. Rather, a settings screen is shown when this button is pressed.
        changeGPSButton = (Button)findViewById(R.id.changeGPS);
        changeGPSButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showGPSOptions();
            }
        });

        totalPowerHandler.removeCallbacks(updatePower);
        totalPowerHandler.postDelayed(updatePower, 1000);  
    }

    private Runnable updateCpu = new Runnable() {
        public void run() {
            float util = cpuMonitor.getUtil();
            int rutil = Math.round(100*util);

            TextView cpuTextView = (TextView)findViewById(R.id.cpuTextView);
            cpuTextView.setText(rutil+"%");      
            cpuHandler.postDelayed(this, updateTime);
            }   
    };


    private Runnable updatePower = new Runnable() 
    {
        public void run()
        {
            //float testPower = componentAnalyzer.getWifiPower();; 

            TextView totalPowerView = (TextView)findViewById(R.id.totalPowerTextView);
            //totalPowerView.setText(testPower+"mW");

            totalPowerHandler.postDelayed(this, 1000);
        }
    };        

2 个答案:

答案 0 :(得分:1)

您是否初始化了context?在context实例化时,当前代码示例中nullComponentAnalyzer

答案 1 :(得分:0)

对于我的观点,您的应用上下文为空(您忘记为当前活动或应用分配上下文的引用)

删除有问题的行的评论,

现在看看这些行,

Bundle extras = getIntent().getExtras();
        if (!instantiated) {
            instantiated = true;
                 componentAnalyzer = new ComponentAnalyzer(context, extras); // Here context is null
        }

根据下面给出的改变,

Bundle extras = getIntent().getExtras();
        if (!instantiated) {
            instantiated = true;
            if(extras != null)
            componentAnalyzer = new ComponentAnalyzer(PowerMonitorActivity.this, extras);
            else Log.e("PowerMonitorActivity","Bundle extras is null");
        }