我有一个Android应用程序的问题,当我在我的手机中测试它立即中断,我得到这个错误:
"java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.massine.final_pgm/com.example.massine.final_pgm.BaseActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.massine.final_pgm.YambaApplication"
我不知道问题出在哪里,我读了类似的帖子,但我无法修复它:这是我的java类和我的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.massine.final_pgm">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.example.massine.final_pgm.BaseActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
和我的java类:
package com.example.massine.final_pgm;
import android.app.Application;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import java.util.List;
import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.Twitter.Status;
public class YambaApplication extends Application implements OnSharedPreferenceChangeListener {
private static final String TAG = YambaApplication.class.getSimpleName();
public Twitter twitter;
private SharedPreferences prefs;
private boolean serviceRunning;
private StatusData statusData;
@Override
public void onCreate() {
super.onCreate();
this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
this.prefs.registerOnSharedPreferenceChangeListener(this);
this.statusData = new StatusData(this);
Log.i(TAG, "onCreated");
}
@Override
public void onTerminate() {
super.onTerminate();
Log.i(TAG, "onTerminated");
}
// Returns the Twitter object
public synchronized Twitter getTwitter() {
if (this.twitter == null) {
String username = this.prefs.getString("username", null);
String password = this.prefs.getString("password", null);
String apiRoot = prefs.getString("apiRoot", "http://yamba.newcircle.com/api");
if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)
&& !TextUtils.isEmpty(apiRoot)) {
this.twitter = new Twitter(username, password);
this.twitter.setAPIRootUrl(apiRoot);
}
}
return this.twitter;
}
// Part of being OnSharedPreferenceChangeListener
public synchronized void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
this.twitter = null;
}
public boolean isServiceRunning() {
return serviceRunning;
}
public void setServiceRunning(boolean serviceRunning) {
this.serviceRunning = serviceRunning;
}
public StatusData getStatusData() {
return statusData;
}
public SharedPreferences getPrefs() {
return prefs;
}
// Connects to the online service and puts the latest statuses into DB.
// Returns the count of new statuses
public synchronized int fetchStatusUpdates() {
Log.d(TAG, "Fetching status updates");
Twitter twitter = this.getTwitter();
if (twitter == null) {
Log.d(TAG, "Twitter connection info not initialized");
return 0;
}
try {
List<Status> statusUpdates = twitter.getFriendsTimeline();
long latestStatusCreatedAtTime = this.getStatusData()
.getLatestStatusCreatedAtTime();
int count = 0;
ContentValues values = new ContentValues();
for (Status status : statusUpdates) {
values.put(StatusData.C_ID, status.getId());
long createdAt = status.getCreatedAt().getTime();
values.put(StatusData.C_CREATED_AT, createdAt);
values.put(StatusData.C_TEXT, status.getText());
values.put(StatusData.C_USER, status.getUser().getName());
Log.d(TAG, "Got update with id " + status.getId() + ". Saving");
this.getStatusData().insertOrIgnore(values);
if (latestStatusCreatedAtTime < createdAt) {
count++;
}
}
Log.d(TAG, count > 0 ? "Got " + count + " status updates"
: "No new status updates");
return count;
} catch (RuntimeException e) {
Log.e(TAG, "Failed to fetch status updates", e);
return 0;
}
}
}
问题的关键是:
yamba = ((YambaApplication) getApplication());
谢谢大家。
答案 0 :(得分:1)
您需要在清单文件中的应用程序元素上指定android:name
。
android:name="com.example.massine.final_pgm.YambaApplication"
没有它,代码会运行,但不会使用YambaApplication类,而是使用导致ClassCastException的默认Application。
根据评论进行编辑以澄清:
<application
android:name="com.example.massine.final_pgm.YambaApplication"
...>