Dav*_*d_D 3 java android casting android-activity
我MainActivity的菜单中有一个项目,我称之为另一项活动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");
}
}
----
---
Run Code Online (Sandbox Code Playgroud)
在这一行:
_app = (CpuSpyApp)getApplication();
findViews();
Run Code Online (Sandbox Code Playgroud)
我正在调用另一个CpuSpyApp类,用几行代码解释:
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();
}
Run Code Online (Sandbox Code Playgroud)
当我单击菜单中的某个项目时,会发生应用程序崩溃,并且logcat会在下面显示错误
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to....
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
Leb*_*vsd 11
在Androidmanifest .xml中
<application
android:name="your.Packagename.CpuSpyApp"
Run Code Online (Sandbox Code Playgroud)