android向后兼容性错误 - java.lang.NoSuchMethoderror

时间:2014-06-06 02:45:03

标签: android xml eclipse android-layout android-sdk-2.3

我想为android 2.2版本提供向后兼容性。我有两个类(NoteList.java和NoteEdit.java),它的主要组件是Navigation抽屉和Actionbar。该应用程序在Android 4.2&上面的版本,但它崩溃(强制关闭)在Android 2.2 ..我已经尝试在清单文件中更改minsdkversion并尝试将支持库(android.support.v7.appcompat)添加到libs文件夹并添加到构建路径,但应用程序继续崩溃。

  

NoteList.java

package com.example.notepad;

import android.app.ActionBar;

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class NoteList extends Activity 
{
    private String[] mPlanetTitles;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ListView mainList;
    private ActionBarDrawerToggle actionBarDrawerToggle;

  @Override
protected void onCreate(Bundle savedInstanceState) 
 {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.notelist);
      mPlanetTitles = getResources().getStringArray(R.array.planets);
      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
      mDrawerList = (ListView) findViewById(R.id.left_drawer);
      final Animation anim = AnimationUtils.loadAnimation(this, R.anim.scale);
      mainList = (ListView)findViewById(R.id.listView1);       
      mainList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPlanetTitles));
      ActionBar actionBar = getActionBar();
      actionBar.show();

      mainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override 
          public void onItemClick(AdapterView<?> adapterView, final View view, final int position, long id) {
              anim.setAnimationListener(new Animation.AnimationListener() {
                  @Override 
                  public void onAnimationStart(Animation animation) { 

                  }
                  @Override 
                  public void onAnimationRepeat(Animation animation) {

                  } 
                  @Override 
                  public void onAnimationEnd(Animation animation) {
                      view.startAnimation(anim);
                      } 
                  }); 
      view.startAnimation(anim); 
      } 
   }); 

      mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item, mPlanetTitles));

      actionBarDrawerToggle = new ActionBarDrawerToggle(this,                 
              mDrawerLayout,         
              R.drawable.ic_launcher, 
              R.string.drawer_open, 
              R.string.drawer_close 
              );
      mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
      getActionBar().setDisplayHomeAsUpEnabled(true); 
  }

  @Override
public boolean onCreateOptionsMenu(Menu menu) {

      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menus, menu);
      return super.onCreateOptionsMenu(menu);
}

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

      if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
          return true;
      }
      switch(item.getItemId())
      {
      case R.id.new_note: 
                        Intent intent = new Intent(NoteList.this,NoteEdit.class);
                        startActivity(intent);

      default: 
             return super.onOptionsItemSelected(item);
      }

  }

}
  

清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notepad"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/appicon"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light" >

        <activity android:name=".NoteList"
             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=".NoteEdit"
             android:label="@string/app_name">           
       </activity>

    </application>

</manifest>
  

错误记录

06-06 07:44:15.508: E/AndroidRuntime(339): FATAL EXCEPTION: main
06-06 07:44:15.508: E/AndroidRuntime(339): java.lang.NoSuchMethodError: com.example.notepad.NoteList.getActionBar
06-06 07:44:15.508: E/AndroidRuntime(339):  at com.example.notepad.NoteList.onCreate(NoteList.java:39)
06-06 07:44:15.508: E/AndroidRuntime(339):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-06 07:44:15.508: E/AndroidRuntime(339):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-06 07:44:15.508: E/AndroidRuntime(339):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-06 07:44:15.508: E/AndroidRuntime(339):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-06 07:44:15.508: E/AndroidRuntime(339):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-06 07:44:15.508: E/AndroidRuntime(339):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 07:44:15.508: E/AndroidRuntime(339):  at android.os.Looper.loop(Looper.java:123)
06-06 07:44:15.508: E/AndroidRuntime(339):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-06 07:44:15.508: E/AndroidRuntime(339):  at java.lang.reflect.Method.invokeNative(Native Method)
06-06 07:44:15.508: E/AndroidRuntime(339):  at java.lang.reflect.Method.invoke(Method.java:521)
06-06 07:44:15.508: E/AndroidRuntime(339):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-06 07:44:15.508: E/AndroidRuntime(339):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-06 07:44:15.508: E/AndroidRuntime(339):  at dalvik.system.NativeStart.main(Native Method)
  

notelist.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/notelist"
    android:orientation="vertical" >

    <android.support.v4.widget.DrawerLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/drawer_layout"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
    <!-- The main content view -->
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    <!-- The navigation drawer -->
        <ListView android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#111"/>

    <RelativeLayout
        android:layout_width="214dp"
        android:layout_height="322dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="48dp" >

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true">

        </ListView>

    </RelativeLayout>   

    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

对于android api&lt;你必须使用getSupportActionBar()而不是getActionBar() 11。

引用文档 &#34; 删除操作栏

您可以通过调用hide()在运行时隐藏操作栏。例如:

ActionBar actionBar = getSupportActionBar(); actionBar.hide();

在API级别11或更高级别 使用getActionBar()方法获取ActionBar。

&#34;

答案 1 :(得分:0)

创建一个操作栏对象并使用它而不是getActionBar和getSupportActionBar ..

ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true); 

还要将appCompat V7库添加到项目中并导入它。