原因:java.lang.NoSuchFieldError:android.support.v7.appcompat
使用此代码:原因:java.lang.NoSuchFieldError:android.support.v7.appcompat,java,android,android-appcompat,Java,Android,Android Appcompat,使用此代码: package com.example.newsapp; 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.Toas
package com.example.newsapp;
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.Toast;
public class MainActivity extends ActionBarActivity {
private Button mTrueButton;
private Button mFalseButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTrueButton = (Button)findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,
R.string.correct_toast,
Toast.LENGTH_SHORT).show();
}
});
mFalseButton = (Button)findViewById(R.id.true_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,
R.string.incorret_toast,
Toast.LENGTH_SHORT).show();
}
});
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这个XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newsapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我得到了以下错误:
12-28 16:10:43.777: I/dalvikvm(13310): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
12-28 16:10:43.777: W/dalvikvm(13310): VFY: unable to resolve virtual method 11426: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
12-28 16:10:43.777: D/dalvikvm(13310): VFY: replacing opcode 0x6f at 0x0000
12-28 16:10:43.777: I/dalvikvm(13310): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
12-28 16:10:43.777: W/dalvikvm(13310): VFY: unable to resolve virtual method 11432: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
12-28 16:10:43.777: D/dalvikvm(13310): VFY: replacing opcode 0x6f at 0x0000
12-28 16:10:43.777: I/dalvikvm(13310): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
12-28 16:10:43.777: W/dalvikvm(13310): VFY: unable to resolve virtual method 9005: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
12-28 16:10:43.777: D/dalvikvm(13310): VFY: replacing opcode 0x6e at 0x000e
12-28 16:10:43.787: D/dalvikvm(13310): DexOpt: couldn't find static field Landroid/support/v7/appcompat/R$styleable;.Toolbar_buttonGravity
12-28 16:10:43.787: W/dalvikvm(13310): VFY: unable to resolve static field 3142 (Toolbar_buttonGravity) in Landroid/support/v7/appcompat/R$styleable;
12-28 16:10:43.787: D/dalvikvm(13310): VFY: replacing opcode 0x60 at 0x00aa
12-28 16:10:43.817: I/dalvikvm(13310): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-28 16:10:43.817: W/dalvikvm(13310): VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-28 16:10:43.817: D/dalvikvm(13310): VFY: replacing opcode 0x6e at 0x0002
12-28 16:10:43.817: I/dalvikvm(13310): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
12-28 16:10:43.817: W/dalvikvm(13310): VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
12-28 16:10:43.817: D/dalvikvm(13310): VFY: replacing opcode 0x6e at 0x0002
12-28 16:10:43.827: D/AndroidRuntime(13310): Shutting down VM
12-28 16:10:43.827: W/dalvikvm(13310): threadid=1: thread exiting with uncaught exception (group=0x418eeda0)
12-28 16:10:43.827: E/AndroidRuntime(13310): FATAL EXCEPTION: main
12-28 16:10:43.827: E/AndroidRuntime(13310): Process: com.example.newsapp, PID: 13310
12-28 16:10:43.827: E/AndroidRuntime(13310): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newsapp/com.example.newsapp.MainActivity}: android.view.InflateException: Binary XML file line #36: Error inflating class android.support.v7.widget.Toolbar
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.app.ActivityThread.access$900(ActivityThread.java:161)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.os.Handler.dispatchMessage(Handler.java:102)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.os.Looper.loop(Looper.java:157)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.app.ActivityThread.main(ActivityThread.java:5356)
12-28 16:10:43.827: E/AndroidRuntime(13310): at java.lang.reflect.Method.invokeNative(Native Method)
12-28 16:10:43.827: E/AndroidRuntime(13310): at java.lang.reflect.Method.invoke(Method.java:515)
12-28 16:10:43.827: E/AndroidRuntime(13310): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
12-28 16:10:43.827: E/AndroidRuntime(13310): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
12-28 16:10:43.827: E/AndroidRuntime(13310): at dalvik.system.NativeStart.main(Native Method)
12-28 16:10:43.827: E/AndroidRuntime(13310): Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class android.support.v7.widget.Toolbar
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.view.LayoutInflater.createView(LayoutInflater.java:626)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.support.v7.app.ActionBarActivityDelegateBase.ensureSubDecor(ActionBarActivityDelegateBase.java:273)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:225)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
12-28 16:10:43.827: E/AndroidRuntime(13310): at com.example.newsapp.MainActivity.onCreate(MainActivity.java:18)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.app.Activity.performCreate(Activity.java:5426)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
12-28 16:10:43.827: E/AndroidRuntime(13310): ... 11 more
12-28 16:10:43.827: E/AndroidRuntime(13310): Caused by: java.lang.reflect.InvocationTargetException
12-28 16:10:43.827: E/AndroidRuntime(13310): at java.lang.reflect.Constructor.constructNative(Native Method)
12-28 16:10:43.827: E/AndroidRuntime(13310): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.view.LayoutInflater.createView(LayoutInflater.java:600)
12-28 16:10:43.827: E/AndroidRuntime(13310): ... 24 more
12-28 16:10:43.827: E/AndroidRuntime(13310): Caused by: java.lang.NoSuchFieldError: android.support.v7.appcompat.R$styleable.Toolbar_buttonGravity
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.support.v7.widget.Toolbar.<init>(Toolbar.java:203)
12-28 16:10:43.827: E/AndroidRuntime(13310): at android.support.v7.widget.Toolbar.<init>(Toolbar.java:190)
12-28 16:10:43.827: E/AndroidRuntime(13310): ... 27 more
12-28 16:10:43.777:I/dalvikvm(13310):找不到方法android.view.ViewGroup.onnestedScrolAccepted,从方法android.support.v7.internal.widget.ActionBarOverlayLayout.onnestedScrolAccepted引用
12-28 16:10:43.777:W/dalvikvm(13310):VFY:无法解析虚拟方法11426:Landroid/view/ViewGroup;。ONNESTEDSCROLACCEPTED(Landroid/视图/视图;Landroid/视图/视图;I)V
12-28 16:10:43.777:D/dalvikvm(13310):VFY:在0x0000处替换操作码0x6f
12-28 16:10:43.777:I/dalvikvm(13310):找不到方法android.view.ViewGroup.onStopNestedScroll,该方法引用自方法android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
12-28 16:10:43.777:W/dalvikvm(13310):VFY:无法解析虚拟方法11432:Landroid/view/ViewGroup;。onStopNestedScroll(Landroid/view/view;)V
12-28 16:10:43.777:D/dalvikvm(13310):VFY:在0x0000处替换操作码0x6f
12-28 16:10:43.777:I/dalvikvm(13310):找不到方法android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll,该方法引用自方法android.support.v7.internal.widget.ActionBaroverLayout.setHideOnContentScrollEnabled
12-28 16:10:43.777:W/dalvikvm(13310):VFY:无法解析虚拟方法9005:Landroid/support/v7/internal/widget/ActionBarOverlayLayout;。stopNestedScroll()V
12-28 16:10:43.777:D/dalvikvm(13310):VFY:在0x000e处替换操作码0x6e
12-28 16:10:43.787:D/dalvikvm(13310):DexOpt:找不到静态字段Landroid/support/v7/appcompat/R$styleable;。工具栏按钮重力
12-28 16:10:43.787:W/dalvikvm(13310):VFY:无法解析Landroid/support/v7/appcompat/R$styleable中的静态字段3142(工具栏按钮重力);
12-28 16:10:43.787:D/dalvikvm(13310):VFY:在0x00aa处替换操作码0x60
12-28 16:10:43.817:I/dalvikvm(13310):找不到方法android.content.res.TypedArray.getChangingConfigurations,该方法引用自方法android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-28 16:10:43.817:W/dalvikvm(13310):VFY:无法解析虚拟方法364:Landroid/content/res/TypedArray;。getChangingConfigurations()I
12-28 16:10:43.817:D/dalvikvm(13310):VFY:在0x0002处替换操作码0x6e
12-28 16:10:43.817:I/dalvikvm(13310):找不到从方法android.support.v7.internal.widget.TintTypedArray.getType引用的方法android.content.res.TypedArray.getType
12-28 16:10:43.817:W/dalvikvm(13310):VFY:无法解析虚拟方法386:Landroid/content/res/TypedArray;。getType(I)I
12-28 16:10:43.817:D/dalvikvm(13310):VFY:在0x0002处替换操作码0x6e
12-28 16:10:43.827:D/AndroidRuntime(13310):关闭虚拟机
12-28 16:10:43.827:W/dalvikvm(13310):threadid=1:线程退出时出现未捕获异常(组=0x418eeda0)
12-28 16:10:43.827:E/AndroidRuntime(13310):致命异常:main
12-28 16:10:43.827:E/AndroidRuntime(13310):进程:com.example.newsapp,PID:13310
12-28 16:10:43.827:E/AndroidRuntime(13310):java.lang.RuntimeException:无法启动活动组件信息{com.example.newsapp/com.example.newsapp.MainActivity}:android.view.InflateException:二进制XML文件行#36:膨胀类android.support.v7.widget.Toolbar时出错
12-28 16:10:43.827:E/AndroidRuntime(13310):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
12-28 16:10:43.827:E/AndroidRuntime(13310):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
12-28 16:10:43.827:E/AndroidRuntime(13310):在android.app.ActivityThread.access$900(ActivityThread.java:161)
12-28 16:10:43.827:E/AndroidRuntime(13310):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
12-28 16:10:43.827:E/AndroidRuntime(13310):位于android.os.Handler.dispatchMessage(Handler.java:102)
12-28 16:10:43.827:E/AndroidRuntime(13310):在android.os.Looper.loop(Looper.java:157)上
12-28 16:10:43.827:E/AndroidRuntime(13310):位于android.app.ActivityThread.main(ActivityThread.java:5356)
12-28 16:10:43.827:E/AndroidRuntime(13310):位于java.lang.reflect.Method.Invokenactive(本机方法)
12-28 16:10:43.827:E/AndroidRuntime(13310):在java.lang.reflect.Method.invoke(Method.java:515)
12-28 16:10:43.827:E/AndroidRuntime(13310):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
12-28 16:10:43.827:E/AndroidRuntime(13310):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
12-28 16:10:43.827:E/AndroidRuntime(13310):在dalvik.system.NativeStart.main(本机方法)
12-28 16:10:43.827:E/AndroidRuntime(13310):由以下原因引起:android.view.InflateException:二进制XML文件行#36:对类android.support.v7.widget.Toolbar进行膨胀时出错
12-28 16:10:43.827:E/AndroidRuntime(13310):在android.view.LayoutInflater.createView(LayoutInflater.java:626)
12-28 16:10:43.827:E/AndroidRuntime(13310):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
12-28 16:10:43.827:E/AndroidRuntime(13310):在android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
12-28 16:10:43.827:E/AndroidRuntime(13310):在android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
12-28 16:10:43.827:E/AndroidRuntime(13310):在android.view.LayoutInflater.inflate(LayoutInflater.java:498)
12-28 16:10:43.827:E/AndroidRuntime(13310):在android.view.LayoutInflater.inflate(LayoutInflater.java:398)
12-28 16:10:43.827:E/AndroidRuntime(13310):在android.view.LayoutInflater.inflate(LayoutInflater.java:354)
12-28 16:10:43.827:E/AndroidRuntime(13310):在android.support.v7.app.ActionBarActivityDelegateBase.ensureSubDecor(ActionBarActivityDelegateBase.java:273)
12-28 16:10:43.827:E/AndroidRuntime(13310):在android.support.v7.app.ActionBarActivityDelegateBase.setContentView
mvn install:install-file -Dfile=/path/to/sdk/extras/android/compatibility/v7/appcompat/libs/android-support-v7-appcompat.jar -DgroupId=com.google.android -DartifactId=support-v7-appcompat -Dversion=r21 -Dpackaging=jar
<dependency>
<groupId>com.google.android</groupId>
<artifactId>support-v7-appcompat</artifactId>
<version>r21</version>
<type>jar</type>
<scope>compile</scope>
</dependency>