如何解决操作栏中的错误

时间:2014-01-29 10:46:43

标签: android android-actionbar android-logcat

我正在我的应用中创建操作栏。但突然app停止工作。我不知道为什么logcat中的错误对我来说很奇怪。

MainActivity -

public class MainActivity extends ActionBarActivity{

    private Button hist, typ, ben, exit;
    Intent i, objIntent;

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

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

    public void addListenerOnButton()
    {
        hist = (Button) findViewById(R.id.hist);
        hist.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                startActivity(new Intent(MainActivity.this, History.class));
                finish();
            }
        });

        typ = (Button) findViewById(R.id.typ);
        typ.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                startActivity(new Intent(MainActivity.this, Types.class));  
                finish();        
            }
        });

        ben = (Button) findViewById(R.id.ben);
        ben.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                startActivity(new Intent(MainActivity.this, Benefits.class));  
                finish();               
            }
        });

        exit = (Button) findViewById(R.id.exit);
        exit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                startActivity(new Intent(MainActivity.this, EndSplash.class));
                finish();
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        objIntent = new Intent(MainActivity.this, PlayAudio.class);
        stopService(objIntent);
    }

    @Override 
    public void onBackPressed() {
        super.onBackPressed();
        objIntent = new Intent(MainActivity.this, PlayAudio.class);
        stopService(objIntent);         
    } 

    @Override 
    public void onResume()
    {
        super.onResume();
        objIntent = new Intent(MainActivity.this, PlayAudio.class);
        startService(objIntent);
    }
}

main.xml中 -

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:hmkcode="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:orderInCategory="100"
        hmkcode:showAsAction="always"
        android:icon="@drawable/ic_launcher"
        android:title="Search"/>
    <item
        android:id="@+id/action_copy"
        android:orderInCategory="100"
        hmkcode:showAsAction="always"
        android:icon="@drawable/ic_launcher"
        android:title="Copy"/>
    <item
        android:id="@+id/action_share"
        android:orderInCategory="100"
        hmkcode:showAsAction="always"
        android:icon="@drawable/ic_launcher"
        android:title="Share"/>
</menu>

Logact -

01-29 05:39:38.349: E/AndroidRuntime(1565): FATAL EXCEPTION: main
01-29 05:39:38.349: E/AndroidRuntime(1565): Process: com.info.abc, PID: 1565
01-29 05:39:38.349: E/AndroidRuntime(1565): java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable
01-29 05:39:38.349: E/AndroidRuntime(1565):     at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:107)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at com.info.abc.MainActivity.onCreate(MainActivity.java:20)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at android.app.Activity.performCreate(Activity.java:5231)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at android.os.Looper.loop(Looper.java:136)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at android.app.ActivityThread.main(ActivityThread.java:5017)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at java.lang.reflect.Method.invoke(Method.java:515)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-29 05:39:38.349: E/AndroidRuntime(1565):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

使用支持库中的类时,请确保从相应的包中导入该类。例如,在应用ActionBar类时:

    使用支持库时
  • android.support.v7.appcompat
  • android.app.ActionBar仅针对API级别11或更高级别开发时。

转到官方页面How to setup support libary

<强>更新

将ActionBar添加到Android 2.x(v7 appcompat库)。这里他们展示了如何使用支持库将操作栏带到旧版本的android

http://hmkcode.com/add-actionbar-to-android-2-3-x/

答案 1 :(得分:0)

你的日志说

java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable

您的Android项目中似乎没有引用AppCompat。来自您的评论

I have added it in libs folder. 

错误。

它是一个图书馆项目。你需要在android项目中引用AppCompat

检查主题使用资源添加库 @

http://developer.android.com/tools/support-library/setup.html