gpt4 book ai didi

java.lang.ClassCastException : ish. message.Message 无法转换为 android.app.Application

转载 作者:行者123 更新时间:2023-11-30 02:40:03 25 4
gpt4 key购买 nike

我正在制作一个应用程序,它抛出“ClassCastException:无法转换为 android.app.Application”。我在 stackoverflow 上提到了各种类似的问题,但没有一个答案能解决我的问题。

这是我的代码:

主要 Activity .java:

public class MainActivity extends ListActivity {

private List<Note> posts;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);


//gets current user
ParseUser currentUser = ParseUser.getCurrentUser();

//checks if a user has already logged in
if (currentUser == null) {

//loads the sign in/sign up page
loadLoginView();
}


//now after a user has logged in...

//creates an array adapter to hold the posts
posts = new ArrayList<Note>();
ArrayAdapter<Note> adapter = new ArrayAdapter<Note>(this,
R.layout.list_item_layout, posts);
setListAdapter(adapter);

//Refreshes posts
refreshPostList();
}



private void loadLoginView() {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}







@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

Note note = posts.get(position);
Intent intent = new Intent(this, EditNoteActivity.class);
intent.putExtra("noteId", note.getId());
intent.putExtra("noteTitle", note.getTitle());
intent.putExtra("noteContent", note.getContent());
startActivity(intent);

}

private void refreshPostList() {

//query gets access to posts in parse
ParseQuery<ParseObject> query = ParseQuery.getQuery("Post");
query.whereEqualTo("author", ParseUser.getCurrentUser());


setProgressBarIndeterminateVisibility(true);

query.findInBackground(new FindCallback<ParseObject>() {

@SuppressWarnings("unchecked")
@Override
public void done(List<ParseObject> postList, ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
// If there are results, update the list of posts
// and notify the adapter
posts.clear();
for (ParseObject post : postList) {
Note note = new Note(post.getObjectId(), post
.getString("title"), post.getString("content"));
posts.add(note);
}
((ArrayAdapter<Note>) getListAdapter())
.notifyDataSetChanged();
} else {

Log.d(getClass().getSimpleName(), "Error: " + e.getMessage());
}

}

});

}
}

Android list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ish.message"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:name=".Message"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".EditNoteActivity"
android:label="@string/title_activity_edit_note" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="ish.message.MainActivity" />
</activity>
<activity
android:name=".LoginActivity"
android:label="@string/title_activity_login" >
</activity>
<activity
android:name=".SignUpActivity"
android:label="@string/title_activity_sign_up" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="ish.message.LoginActivity" />
</activity>
<activity
android:name=".Users"
android:label="@string/title_activity_users" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="ish.message.MainActivity" />
</activity>
<activity
android:name=".Message"
android:label="@string/title_activity_message" >
</activity>
<activity
android:name=".EditTextMessage"
android:label="@string/title_activity_edit_text_message" >
</activity>
</application>

Logcat 跟踪:

09-18 21:24:44.507: E/AndroidRuntime(19919): FATAL EXCEPTION: main
09-18 21:24:44.507: E/AndroidRuntime(19919): java.lang.RuntimeException: Unable to instantiate application ish.message.Message: java.lang.ClassCastException: ish.message.Message cannot be cast to android.app.Application
09-18 21:24:44.507: E/AndroidRuntime(19919): at android.app.LoadedApk.makeApplication(LoadedApk.java:501)
09-18 21:24:44.507: E/AndroidRuntime(19919): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4263)
09-18 21:24:44.507: E/AndroidRuntime(19919): at android.app.ActivityThread.access$1400(ActivityThread.java:143)
09-18 21:24:44.507: E/AndroidRuntime(19919): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1301)
09-18 21:24:44.507: E/AndroidRuntime(19919): at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 21:24:44.507: E/AndroidRuntime(19919): at android.os.Looper.loop(Looper.java:137)
09-18 21:24:44.507: E/AndroidRuntime(19919): at android.app.ActivityThread.main(ActivityThread.java:4960)
09-18 21:24:44.507: E/AndroidRuntime(19919): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 21:24:44.507: E/AndroidRuntime(19919): at java.lang.reflect.Method.invoke(Method.java:511)
09-18 21:24:44.507: E/AndroidRuntime(19919): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
09-18 21:24:44.507: E/AndroidRuntime(19919): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
09-18 21:24:44.507: E/AndroidRuntime(19919): at dalvik.system.NativeStart.main(Native Method)
09-18 21:24:44.507: E/AndroidRuntime(19919): Caused by: java.lang.ClassCastException: ish.message.Message cannot be cast to android.app.Application
09-18 21:24:44.507: E/AndroidRuntime(19919): at android.app.Instrumentation.newApplication(Instrumentation.java:997)
09-18 21:24:44.507: E/AndroidRuntime(19919): at android.app.Instrumentation.newApplication(Instrumentation.java:982)
09-18 21:24:44.507: E/AndroidRuntime(19919): at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
09-18 21:24:44.507: E/AndroidRuntime(19919): ... 11 more

如有需要,请随时询问更多详情。

最佳答案

AndroidMenifest.xml 中的 Application 标记中删除 android:name=".Message"

android:name=".Message"Application 标签中意味着你有一个 extends Application 类。

关于java.lang.ClassCastException : ish. message.Message 无法转换为 android.app.Application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25917983/

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