为什么我的Android应用程序强制关闭显示错误膨胀类Button? 这是MainActivity
package com.example.samyak.lovecalculator;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView res = (TextView) findViewById(R.id.textView);
final EditText e1 = (EditText) findViewById(R.id.name1);
final EditText e2 = (EditText) findViewById(R.id.name2);
Button cal = (Button) findViewById(R.id.button);
cal.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
res.setText("");
Editable n1 = e1.getText();
Editable n2 = e2.getText();
String concat = String.valueOf(n1).concat(String.valueOf(n2)).toUpperCase();
if ((n1.toString().trim().length() == 0) || (n2.toString().trim().length() == 0)) {
Toast.makeText(MainActivity.this, "Please fill both the fields", Toast.LENGTH_LONG).show();
} else {
int sum = 0;
for (int i = 0; i < concat.length(); i++) {
char character = concat.charAt(i);
int ascii = (int) character;
sum += ascii;
}
res.setText("The love Between " + n1 + " and " + n2 + " is " + sum % 100 + "%");
}
}
});
}
}
这是XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/love"
tools:context="com.example.samyak.lovecalculator.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:hint="Name"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="106dp"
android:id="@+id/name1" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:hint="Name"
android:ems="10"
android:layout_centerVertical="true"
android:layout_alignEnd="@+id/name1"
android:id="@+id/name2" />
<Button
android:text="Calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_below="@+id/name2"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:id="@+id/button"
style="@style/Widget.AppCompat.Button.Colored"
android:elevation="0dp"
android:visibility="visible"
android:textColorLink="?android:attr/actionModeSelectAllDrawable" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_below="@+id/button"
android:layout_alignParentStart="true"
android:layout_marginTop="35dp" />
</RelativeLayout>
在运行时错误发生时,它在编译时没有显示错误。 错误如下:
02/11 23:20:25: Launching app
$ adb push /Users/samyak/AndroidStudioProjects/LoveCalculator/app/build/outputs/apk/app-debug.apk /data/local/tmp/com.example.samyak.lovecalculator
$ adb shell pm install -r "/data/local/tmp/com.example.samyak.lovecalculator"
Success
$ adb shell am start -n "com.example.samyak.lovecalculator/com.example.samyak.lovecalculator.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Waiting for process to come online
Waiting for process to come online
Connected to process 2383 on device Nexus_5_API_24 [emulator-5554]
I/art: Not late-enabling -Xcheck:jni (already on)
W/art: Unexpected CPU variant for X86 using defaults: x86
W/System: ClassLoader referenced unknown path: /data/app/com.example.samyak.lovecalculator-1/lib/x86
[ 02-11 23:20:27.575 1512: 1536 D/ ]
HostConnection::get() New Host Connection established 0x8c625140, tid 1536
I/InstantRun: Instant Run Runtime started. Android package is com.example.samyak.lovecalculator, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/com.example.samyak.lovecalculator-1/lib/x86
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
D/AndroidRuntime: Shutting down VM
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.samyak.lovecalculator, PID: 2383
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.samyak.lovecalculator/com.example.samyak.lovecalculator.MainActivity}: android.view.InflateException: Binary XML file line #37: Binary XML file line #37: Error inflating class Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.view.InflateException: Binary XML file line #37: Binary XML file line #37: Error inflating class Button
Caused by: android.view.InflateException: Binary XML file line #37: Error inflating class Button
Caused by: android.content.res.Resources$NotFoundException: Can't find ColorStateList from drawable resource ID #0x10803e8
at android.content.res.ResourcesImpl.loadColorStateList(ResourcesImpl.java:840)
at android.content.res.Resources.loadColorStateList(Resources.java:998)
at android.content.res.TypedArray.getColorStateList(TypedArray.java:531)
at android.widget.TextView.<init>(TextView.java:1084)
at android.widget.Button.<init>(Button.java:109)
at android.widget.Button.<init>(Button.java:105)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:66)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:62)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1021)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1080)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.samyak.lovecalculator.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Application terminated.