4

I have an item in my MainActivity's menu in which I call another activity called HomeActivity:

public class HomeActivity extends Activity
{
  private static final String TAG = "CpuSpy";

  private CpuSpyApp _app = null;

  // the views
  private LinearLayout    _uiStatesView = null;
  private TextView        _uiAdditionalStates = null;
  private TextView        _uiTotalStateTime = null;
  private TextView        _uiHeaderAdditionalStates = null;
  private TextView        _uiHeaderTotalStateTime = null;
  private TextView        _uiStatesWarning = null;
  private TextView        _uiKernelString = null;

  /** whether or not we're updating the data in the background */
  private boolean     _updatingData = false;

  /** Initialize the Activity */
  @Override public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);

      // inflate the view, stash the app context, and get all UI elements
      setContentView(R.layout.home_layout);
      _app = (CpuSpyApp)getApplication();

      // set title to version string
      //setTitle(getResources().getText(R.string.app_name) + " v" +
      //getResources().getText(R.string.version_name));

      // see if we're updating data during a config change (rotate screen)
      if (savedInstanceState != null) {
          _updatingData = savedInstanceState.getBoolean("updatingData");
      }
  }
----
---

At this lines:

_app = (CpuSpyApp)getApplication();
  findViews();

I'm calling another CpuSpyApp class which explained in a few lines of code:

public class CpuSpyApp extends Application {

  private static final String KERNEL_VERSION_PATH = "/proc/version";

  private static final String TAG = "CpuSpyApp";

  private static final String PREF_NAME = "CpuSpyPreferences";
  private static final String PREF_OFFSETS = "offsets";

  /** the long-living object used to monitor the system frequency states */
  private CpuStateMonitor _monitor = new CpuStateMonitor();

  private String _kernelVersion = "";

  /**
   * On application start, load the saved offsets and stash the
   * current kernel version string
   */
  @Override public void onCreate(){
      loadOffsets();
      updateKernelVersion();
  }

When I click on an item of my menu, the Application crash happens and the logcat shows me the error below

Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to....

How can I solve the problem?

4

1 回答 1

13

在 Androidmanifest .xml 中

 <application
        android:name="your.Packagename.CpuSpyApp"
于 2013-09-30T09:52:33.050 回答