Android:扩展ActionBarActivity使应用程序崩溃

时间:2014-08-05 15:03:36

标签: java android intellij-idea

我在我的应用程序中使用v7-appcompat作为库,我正在使用IntelliJ-IDEA并在项目目录中导入appcompat库。该库已经集成,因为我得到自动完成的建议,项目没有显示任何错误,我正在使用主题" Theme.AppCompat.Light.DarkActionBar"这也正常工作当我在模拟器上运行应用程序时出现问题,当实现" ActionBarActivity"触发了其他实现"活动"在实现AppCompat主题的情况下正常运行。

此活动运行良好:

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ShareActionProvider;


public class home extends Activity{
private ShareActionProvider mShareActionProvider;
Button btnPoetry;
Button btnProse;
Button btnBiography;
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    btnPoetry = (Button) findViewById(R.id.btnPoetry);
    btnPoetry.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(home.this, poetry.class);
            startActivity(i);
        }
    });
    btnProse = (Button) findViewById(R.id.btnProse);
    btnProse.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(home.this, prose.class);
            startActivity(i);
        }
    });
    btnBiography = (Button) findViewById(R.id.btnBiography);
    btnBiography.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(home.this, biography.class);
            startActivity(i);
        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.main_menu, menu);

    MenuItem item = menu.findItem(R.id.menu_item_share);
    mShareActionProvider = (ShareActionProvider) item.getActionProvider();
    mShareActionProvider.setShareIntent(getDefaultIntent());
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.settings:
            break;
        case R.id.legend:
            break;
        case R.id.aboutUs:
            break;
    }
    return super.onOptionsItemSelected(item);
}

private Intent getDefaultIntent(){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    intent.putExtra(Intent.EXTRA_TEXT, "Extra Text");
    return intent;
}
}

像这些崩溃的活动:

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ShareActionProvider;


public class biography extends ActionBarActivity{
ShareActionProvider mShareActionProvider;
Button btnPictures;
Button btnLW;
Button btnLN;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.biography);

    setUpActionBar();

    btnPictures = (Button) findViewById(R.id.btnPictures);
    btnPictures.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(biography.this, pictures.class);
            startActivity(i);
        }
    });
    btnLW = (Button) findViewById(R.id.btnLW);
    btnLW.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(biography.this, literature.class);
            startActivity(i);
        }
    });
    btnLN = (Button) findViewById(R.id.btnLN);
    btnLN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(biography.this, lifetimes.class);
            startActivity(i);
        }
    });
}

private void setUpActionBar(){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }else{
        android.support.v7.app.ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.main_menu, menu);

    MenuItem item = menu.findItem(R.id.menu_item_share);
    mShareActionProvider = (ShareActionProvider) item.getActionProvider();
    mShareActionProvider.setShareIntent(getDefaultIntent());
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.settings:
            break;
        case R.id.legend:
            break;
        case R.id.aboutUs:
            break;
    }
    return super.onOptionsItemSelected(item);
}

private Intent getDefaultIntent(){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    intent.putExtra(Intent.EXTRA_TEXT, "Extra Text");
    return intent;
}

}

Styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.BaseStyle" parent="@style/Theme.AppCompat.Light.DarkActionBar">

</style>
<style name="CustomButtonStyle">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_centerHorizontal">true</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:capitalize">characters</item>
    <item name="android:typeface">sans</item>
    <item name="android:shadowDx">1.2</item>
    <item name="android:shadowDy">1.2</item>
    <item name="android:shadowRadius">2</item>
    <item name="android:gravity">center</item>
    <item name="android:shadowColor">#000000</item>
    <item name="android:color">#669900</item>
    <item name="android:textSize">20sp</item>
</style>
<style name="CustomTextViewStyle">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">#000000</item>
    <item name="android:maxLines">80000</item>
    <item name="android:paddingLeft">3dp</item>
    <item name="android:paddingTop">5dp</item>
    <item name="android:paddingRight">3dp</item>
</style>
</resources>

logcat的:

08-05 19:35:11.810    1153-1153/com.faizanbashir.zindarud W/dalvikvm﹕ VFY: unable to           resolve static field 1630 (ActionBarWindow) in Landroid/support/v7/appcompat/R$styleable;
08-05 19:35:11.810    1153-1153/com.faizanbashir.zindarud D/dalvikvm﹕ VFY: replacing opcode 0x62 at 0x0004
08-05 19:35:11.829    1153-1153/com.faizanbashir.zindarud D/AndroidRuntime﹕ Shutting down VM
08-05 19:35:11.829    1153-1153/com.faizanbashir.zindarud W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x409961f8)
08-05 19:35:11.859    1153-1153/com.faizanbashir.zindarud E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable
        at  android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:104)
        at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
        at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
        at com.faizanbashir.zindarud.poetry.onCreate(poetry.java:24)
        at android.app.Activity.performCreate(Activity.java:4465)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
        at android.app.ActivityThread.access$600(ActivityThread.java:122)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4340)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        at dalvik.system.NativeStart.main(Native Method)

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.faizanbashir.zindarud"
      android:versionCode="1"
      android:versionName="1.0">
<uses-sdk android:minSdkVersion="8"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
                 android:maxSdkVersion="19" />
<application android:label="@string/app_name" android:icon="@drawable/allama" android:theme="@style/Theme.BaseStyle">
    <activity android:name="main"
              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=".home"
              android:label="@string/app_name">
    </activity>
    <activity android:name=".poetry"
              android:label="@string/Poetry">
    </activity>
    <activity android:name=".prose"
              android:label="@string/Prose">
    </activity>
    <activity android:name=".persian"
              android:label="@string/persianPoetry">
    </activity>
    <activity android:name=".urdu"
              android:label="@string/urduPoetry">
    </activity>
    <activity android:name=".biography"
              android:label="@string/Biography">
    </activity>
    <activity android:name=".pictures"
              android:label="@string/Pictures">
    </activity>
    <activity android:name=".literature"
              android:label="@string/LiteraryWorks">
    </activity>
    <activity android:name=".lifetimes"
              android:label="@string/LN">
    </activity>
</application>
</manifest>

1 个答案:

答案 0 :(得分:1)

请务必执行以下操作(from Support Library Setup):

  

右键单击库项目文件夹,然后选择Build Path&gt;   配置构建路径。

     

在“订购和导出”选项卡中,检查刚添加的.jar文件   构建路径,因此它们可用于依赖于此的项目   图书馆项目。例如,appcompat项目需要您   导出android-support-v4.jar和   android-support-v7-appcompat.jar文件。

     

取消选中Android依赖项。