ActionBarActivity catch an error on Phone

我的梦境 提交于 2019-12-21 17:31:23

问题


** I was recomended to use ActionBar Activity** Here is the previous code

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;


public class MainActivity extends Activity  {

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

    }

I wrote new application and followed advice.

import android.os.Bundle;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.ActionBarActivity;


    public class MainActivity extends ActionBarActivity {

          @Override
          public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ActionBar actionBar =getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);

            setContentView(R.layout.activity_main);
          }

          @Override
          public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
          }

        }

if I use ACtionBarActivity instead of Activity
I catch an error message on phone, when I try to run it.
Please tell me what I do wrong

info from Logcat

    07-27 15:14:19.942: I/Process(21715): Sending signal. PID: 21715 SIG: 9
07-27 15:34:38.521: W/dalvikvm(23579): Unable to resolve superclass of Lcom/example/project/MainActivity; (532)
07-27 15:34:38.521: W/dalvikvm(23579): Link of class 'Lcom/example/project/MainActivity;' failed
07-27 15:34:38.521: D/AndroidRuntime(23579): Shutting down VM
07-27 15:34:38.521: W/dalvikvm(23579): threadid=1: thread exiting with uncaught exception (group=0x40ae5210)
07-27 15:34:38.521: E/AndroidRuntime(23579): FATAL EXCEPTION: main
07-27 15:34:38.521: E/AndroidRuntime(23579): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.project/com.example.project.MainActivity}: java.lang.ClassNotFoundException: com.example.project.MainActivity
07-27 15:34:38.521: E/AndroidRuntime(23579):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
07-27 15:34:38.521: E/AndroidRuntime(23579):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
07-27 15:34:38.521: E/AndroidRuntime(23579):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-27 15:34:38.521: E/AndroidRuntime(23579):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
07-27 15:34:38.521: E/AndroidRuntime(23579):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 15:34:38.521: E/AndroidRuntime(23579):    at android.os.Looper.loop(Looper.java:137)
07-27 15:34:38.521: E/AndroidRuntime(23579):    at android.app.ActivityThread.main(ActivityThread.java:4441)
07-27 15:34:38.521: E/AndroidRuntime(23579):    at java.lang.reflect.Method.invokeNative(Native Method)
07-27 15:34:38.521: E/AndroidRuntime(23579):    at java.lang.reflect.Method.invoke(Method.java:511)
07-27 15:34:38.521: E/AndroidRuntime(23579):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
07-27 15:34:38.521: E/AndroidRuntime(23579):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
07-27 15:34:38.521: E/AndroidRuntime(23579):    at dalvik.system.NativeStart.main(Native Method)
07-27 15:34:38.521: E/AndroidRuntime(23579): Caused by: java.lang.ClassNotFoundException: com.example.project.MainActivity
07-27 15:34:38.521: E/AndroidRuntime(23579):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
07-27 15:34:38.521: E/AndroidRuntime(23579):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-27 15:34:38.521: E/AndroidRuntime(23579):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-27 15:34:38.521: E/AndroidRuntime(23579):    at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
07-27 15:34:38.521: E/AndroidRuntime(23579):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
07-27 15:34:38.521: E/AndroidRuntime(23579):    ... 11 more
07-27 15:34:38.531: I/Process(23579): Sending signal. PID: 23579 SIG: 9

edit and export screenshot http://i.stack.imgur.com/MNfc1.jpg

screenshot of 1-7

http://i.stack.imgur.com/NTpP3.jpg


回答1:


I've got it worked after moving "Android Private Libraries" and "Android Dependencies" to the top of the list in Order and Export in my application project. Also I dont have android-support-v7-appcompat in this list. And in the android-support-v7-appcompat project I dont have support jars in the Order and Export list, but "Android Private Libraries" is checked. Please, check also the links below:

Setting up ActionBarCompat support library in Eclipse

Implementing Action Bar using ActionBarCompat support library in Android




回答2:


Make sure the support library is added to the project and exported. You can do so in Eclipse by viewing the properties of your project, clicking Java Build Path, then the Order and Export tab. Then make sure Android Private Libraries is checked.

Did you follow these steps: http://developer.android.com/tools/support-library/setup.html#




回答3:


Had the exact same stack trace as you with Android Studio. I think it is caused by Android not being able to find a suitable theme for ActionBarActivity.

Anyway, this is how I solved it:

Add the following to your MainActivity in AndroidManifest.xml:

android:theme="@style/Theme.AppCompat.Light"

If it works now - great! If Android Studio cannot find the theme: Copy the files

sdk/extras/android/support/v7/appcompat/res/values/themes.xml
sdk/extras/android/support/v7/appcompat/res/values/themes_base.xml

to the res/values/ folder in your project.



来源:https://stackoverflow.com/questions/17897247/actionbaractivity-catch-an-error-on-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!