0

这是一个完美运行的类,直到我想添加更多功能,代码编译然后在执行时立即崩溃。
问题在于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);
        }
    };        
4

2 回答 2

1

你初始化了context吗?在当前的代码示例context中是实例化的null时候。ComponentAnalyzer

于 2012-06-19T09:50:55.320 回答
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");
        }
于 2012-06-19T09:51:15.277 回答