gpt4 book ai didi

java - android.app.Application 无法转换 Yamba 应用程序?

转载 作者:行者123 更新时间:2023-12-01 10:34:28 26 4
gpt4 key购买 nike

我的 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 类和我的 list :

<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());

谢谢大家。

最佳答案

您需要在 list 文件中的应用程序元素上指定 android:name

android:name="com.example.massine.final_pgm.YambaApplication"

如果没有它,代码会运行,但不会使用 YambaApplication 类,而是使用导致 ClassCastException 的默认应用程序。

根据评论进行编辑以进行澄清:

<application
android:name="com.example.massine.final_pgm.YambaApplication"
...>

关于java - android.app.Application 无法转换 Yamba 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34862908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com