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

时间:2014-09-18 16:34:36

标签: java android casting classcastexception runtimeexception

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

这是我的代码:

MainActivity.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 Manifest:

<?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

如果需要,请随时询问更多详细信息。

1 个答案:

答案 0 :(得分:1)

android:name=".Message"

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

Application表示您有一个extends Application类的课程。