简体   繁体   中英

App crashes in 2.2 after adding appcompat

I try to use appcompat for support actionbar in android 2.2 but when I build app , crash First I add AppCompat as library. This is my Splash.Class ;

package com.skyline.faceball;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;

public class Splash extends ActionBarActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        startActivity(new Intent(Splash.this, Main.class));  
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.splash, menu);
        return true;
    }

}

and in manifast I change that to :

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat" >
        <activity
            android:name="com.skyline.faceball.Splash"
            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="com.skyline.faceball.Main"
            android:label="@string/title_activity_main" >
        </activity>
    </application>

</manifest>

and see my Log :

09-26 01:08:40.779: E/AndroidRuntime(501): FATAL EXCEPTION: main
09-26 01:08:40.779: E/AndroidRuntime(501): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.skyline.faceball/com.skyline.faceball.Splash}: java.lang.ClassNotFoundException: com.skyline.faceball.Splash in loader dalvik.system.PathClassLoader[/data/app/com.skyline.faceball-1.apk]
09-26 01:08:40.779: E/AndroidRuntime(501):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
09-26 01:08:40.779: E/AndroidRuntime(501):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-26 01:08:40.779: E/AndroidRuntime(501):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-26 01:08:40.779: E/AndroidRuntime(501):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-26 01:08:40.779: E/AndroidRuntime(501):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 01:08:40.779: E/AndroidRuntime(501):  at android.os.Looper.loop(Looper.java:123)
09-26 01:08:40.779: E/AndroidRuntime(501):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-26 01:08:40.779: E/AndroidRuntime(501):  at java.lang.reflect.Method.invokeNative(Native Method)
09-26 01:08:40.779: E/AndroidRuntime(501):  at java.lang.reflect.Method.invoke(Method.java:521)
09-26 01:08:40.779: E/AndroidRuntime(501):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-26 01:08:40.779: E/AndroidRuntime(501):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-26 01:08:40.779: E/AndroidRuntime(501):  at dalvik.system.NativeStart.main(Native Method)
09-26 01:08:40.779: E/AndroidRuntime(501): Caused by: java.lang.ClassNotFoundException: com.skyline.faceball.Splash in loader dalvik.system.PathClassLoader[/data/app/com.skyline.faceball-1.apk]
09-26 01:08:40.779: E/AndroidRuntime(501):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
09-26 01:08:40.779: E/AndroidRuntime(501):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
09-26 01:08:40.779: E/AndroidRuntime(501):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
09-26 01:08:40.779: E/AndroidRuntime(501):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
09-26 01:08:40.779: E/AndroidRuntime(501):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
09-26 01:08:40.779: E/AndroidRuntime(501):  ... 11 more

andmy Main.Class

package com.skyline.faceball;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;

public class Main extends ActionBarActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setTitle("Main");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

How Can I delete this problem ? thanks.

Try this inplace of:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.splash, menu);  
        return true;  




 public boolean onCreateOptionsMenu(Menu menu) {  
        // Removing this prevents the crash: 
        getMenuInflater().inflate(R.menu.main, menu); 
        MenuItem searchItem = menu.findItem(R.id.action_search);
        searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        return true;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM