由于Android Studio中运行时出现异常,导致Android应用程序崩溃



这是我的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatAutoCompleteTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/simple_sales_inventory_using_android"
android:textColor="@color/colorAccent" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/ed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:importantForAutofill="no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/product_name" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/ed2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/price" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/ed3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/quantity" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/txtsub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/colorPrimaryDark"
android:ems="10"
android:enabled="false"
android:textColor="@string/fff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/subtotal" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/txtpay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/colorPrimaryDark"
android:ems="10"
android:textColor="@string/fff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/payment" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/txtbal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/colorPrimaryDark"
android:ems="10"
android:enabled="false"
android:textColor="@string/fff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/balance" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="@string/add" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:id="@+id/tb1"
android:layout_width="match_parent"
android:layout_height="360dp"
android:stretchColumns="*">
<TableRow
android:id="@+id/tbrow1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/product_name"
android:textAlignment="center" />
<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/price"
android:textAlignment="center" />
<TextView
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/qty"
android:textAlignment="center" />
<TextView
android:id="@+id/t4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/total"
android:textAlignment="center" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>

我在XML代码或Java代码中没有看到任何错误。

这是我的Java代码:

package com.example.simpleinventory;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<String> data = new ArrayList<>();
private ArrayList<String> data1 = new ArrayList<>();
private ArrayList<String> data2 = new ArrayList<>();
private ArrayList<String> data3 = new ArrayList<>();
EditText ed1,ed2,ed3,ed4,ed5,ed6;
Button b1;
public MainActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.activity_main);
ed1=findViewById(R.id.ed1);
ed2=findViewById(R.id.ed2);
ed3=findViewById(R.id.ed3);
ed4=findViewById(R.id.txtsub);
ed5=findViewById(R.id.txtpay);
ed6=findViewById(R.id.txtbal);
b1=findViewById(R.id.btn1);
ed5.addTextChangedListener (new TextWatcher () {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
int subtotal = Integer.parseInt(ed4.getText().toString());
int pay = Integer.parseInt(ed5.getText().toString());
int bal = pay - subtotal;
ed6.setText(String.valueOf(bal));
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
add();
}
});
}
public void add() {
int tot;
String prodname = ed1.getText().toString();
int price = Integer.parseInt(ed2.getText().toString());
int qty = Integer.parseInt(ed3.getText().toString());
tot = price*qty;
data.add(prodname);
data1.add(String.valueOf(price));
data2.add(String.valueOf(qty));
data3.add(String.valueOf(tot));
TableLayout table = findViewById(R.id.tb1);
TableRow row = new TableRow(this);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
TextView t3 = new TextView(this);
TextView t4 = new TextView(this);
String total;
int sum =0;
for(int i=0; i<data.size(); i++)
{
String pname = data.get(i);
String prc = data1.get(i);
String qtyy = data2.get(i);
total = data3.get(i);
t1.setText(pname);
t2.setText(prc);
t3.setText(qtyy);
t4.setText(total);
sum = sum + Integer.parseInt(data3.get(i));
}
row.addView(t1);
row.addView(t2);
row.addView(t3);
row.addView(t4);
table.addView(row);
ed4.setText(String.valueOf(sum));
ed1.setText("");
ed2.setText("");
ed3.setText("");
ed1.requestFocus();
}
}

我得到了0个编译错误,但我在Logcat中得到了一个运行时错误。

错误如下:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simpleinventory/com.example.simpleinventory.MainActivity}: android.view.InflateException: Binary XML file line #88 in com.example.simpleinventory:layout/activity_main: Binary XML file line #88 in com.example.simpleinventory:layout/activity_main: Error inflating class EditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3271)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2017)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7403)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
Caused by: android.view.InflateException: Binary XML file line #88 in com.example.simpleinventory:layout/activity_main: Binary XML file line #88 in com.example.simpleinventory:layout/activity_main: Error inflating class EditText
Caused by: android.view.InflateException: Binary XML file line #88 in com.example.simpleinventory:layout/activity_main: Error inflating class EditText
Caused by: android.content.res.Resources$NotFoundException: File #fff from drawable resource ID #0x7f0c001e: .xml extension required
at android.content.res.ResourcesImpl.loadComplexColorForCookie(ResourcesImpl.java:1195)
at android.content.res.ResourcesImpl.loadComplexColorFromName(ResourcesImpl.java:1013)
at android.content.res.ResourcesImpl.loadColorStateList(ResourcesImpl.java:1092)
at android.content.res.Resources.loadColorStateList(Resources.java:1062)
at android.content.res.TypedArray.getColorStateList(TypedArray.java:599)
at android.widget.TextView.readTextAppearance(TextView.java:3961)
at android.widget.TextView.<init>(TextView.java:1064)
at android.widget.EditText.<init>(EditText.java:88)
at android.widget.EditText.<init>(EditText.java:84)
at androidx.appcompat.widget.AppCompatEditText.<init>(AppCompatEditText.java:74)
at androidx.appcompat.widget.AppCompatEditText.<init>(AppCompatEditText.java:69)
at androidx.appcompat.app.AppCompatViewInflater.createEditText(AppCompatViewInflater.java:209)
at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:127)
at androidx.appcompat.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1551)
at androidx.appcompat.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1602)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1061)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1126)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at android.view.LayoutInflater.inflate(LayoutInflater.java:481)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.example.simpleinventory.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:7809)
2020-11-02 13:08:17.527 8788-8788/com.example.simpleinventory E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:7798)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3246)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2017)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7403)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)

您根本没有设置内容,因此所有findViewById都返回null。。。

super.onCreate调用之后(在所有findViewById调用之前(添加setContentView方法,指向发布的XML文件(R.layout.filename(

这是从activity_main.xml文件的第88行报告的,我看到您的textColor引用了一个字符串,您能验证这一部分是否正确吗。

android:textColor="@string/fff"

它应该是一种颜色,即:

android:textColor="@color/colorAccent"

你能发布你的字符串.xml吗?