知道为什么会出现此错误的任何提示:
android.view.InflateException
keithcolclough.stonestokilograms.stonetokilogramsconverter.MainActivity.onCreate 引起原因:android.view.InflateException:
at android.view.LayoutInflater.inflate (LayoutInflater.java:551)
at android.view.LayoutInflater.inflate (LayoutInflater.java:429)
at android.view.LayoutInflater.inflate (LayoutInflater.java:380)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView (AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView (AppCompatActivity.java:139)
at keithcolclough.stonestokilograms.stonetokilogramsconverter.MainActivity.onCreate (MainActivity.java:268)
at android.app.Activity.performCreate (Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3206)
这是XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Enter the amount you want to convert"
android:textAlignment="center"
android:textColor="@color/browser_actions_text_color"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView3" />
<EditText
android:id="@+id/stoneText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="64dp"
android:ems="10"
android:hint="Amount in Stone"
android:inputType="textPersonName"
android:textAlignment="viewStart"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<EditText
android:id="@+id/KgText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="64dp"
android:ems="10"
android:hint="Amount in KG"
android:inputType="textPersonName"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/convertToKgButton" />
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-2327915784489185/1078421100"
ads:layout_constraintBottom_toBottomOf="parent"
ads:layout_constraintEnd_toEndOf="parent"
ads:layout_constraintStart_toStartOf="parent"
ads:layout_constraintTop_toBottomOf="@+id/convertToStoneButton"
ads:layout_constraintVertical_bias="1.0"></com.google.android.gms.ads.AdView>
<ImageButton
android:id="@+id/imageView3"
android:layout_width="327dp"
android:layout_height="118dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#ffffff"
android:onClick="imageClick"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/fitness" />
<Button
android:id="@+id/convertToKgButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="77dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="77dp"
android:background="@color/colorPrimary"
android:elevation="100dp"
android:onClick="convertStoneClick"
android:text="Convert to KG"
android:textColor="@color/browser_actions_bg_grey"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/stoneText" />
<Button
android:id="@+id/convertToStoneButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="77dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="77dp"
android:layout_marginBottom="8dp"
android:background="@color/colorPrimary"
android:elevation="100dp"
android:onClick="convertKgClick"
android:text="Convert to Stone"
android:textColor="@color/browser_actions_bg_grey"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/KgText"
app:layout_constraintVertical_bias="0.0" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintGuide_begin="513dp" />
<android.support.constraint.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="199dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/tipButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="2dp"
android:clickable="true"
android:focusable="true"
android:onClick="hintClick"
app:backgroundTint="@color/colorPrimaryDark"
app:layout_constraintBottom_toTopOf="@+id/adView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/convertToStoneButton"
app:srcCompat="@drawable/questionmark" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
这是主要的活动代码。
软件包keithcolclough.stonestokilograms.stonetokilogramsconverter;
import android.content.Context;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import java.util.Locale;
import java.util.Random;
// An app that converts stones and KG weights for the user.
public class MainActivity extends AppCompatActivity {
Toast myToast;
public void hintClick(View view){
// Play a pop noise to signal button has been clicked
final MediaPlayer mp = MediaPlayer.create(this, R.raw.pop);
mp.start();
//Picks a random number a then pops a random toast depending on that number
Random rand = new Random();
int randomNum = rand.nextInt((10 - 1) + 1) + 1;
if (randomNum == 1) {
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, "The average man weighs 195 pounds (88KG)", Toast.LENGTH_SHORT);
myToast.show();
}
else if (randomNum == 2) {
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, "The average woman weighs 168 pounds (76KG)", Toast.LENGTH_SHORT);
myToast.show();
}
else if (randomNum == 3) {
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, "One study showed that short sleep was linked to 89% increased risk of obesity in children, and 55% in adults.", Toast.LENGTH_LONG);
myToast.show();
}
else if (randomNum == 4) {
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, "Studies show that people who eat the most fish have a lower risk of all sorts of diseases, including heart disease, dementia and depression.", Toast.LENGTH_LONG);
myToast.show();
}
else if (randomNum == 5) {
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, "Coffee is high in antioxidants, and studies show that coffee drinkers live longer, and have a reduced risk of type 2 diabetes.", Toast.LENGTH_LONG);
myToast.show();
}
else if (randomNum == 6) {
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, "Despite being high in fat, nuts are incredibly nutritious and healthy.\nThey are loaded with magnesium, vitamin E, fiber and various other nutrients.", Toast.LENGTH_LONG);
myToast.show();
}
else if (randomNum == 7) {
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, "A good way to improve gut health, is to eat probiotic foods (like live yogurt and sauerkraut), take probiotic supplements, and eat plenty of fiber.", Toast.LENGTH_LONG);
myToast.show();
}
else if (randomNum == 8) {
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, "The best time to drink water is half an hour before meals. One study showed that half a liter of water, 30 minutes before each meal, increased weight loss by 44%", Toast.LENGTH_LONG);
myToast.show();
}
else if (randomNum == 9) {
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, "Studies show that people who eat the most vegetables and fruits live longer, and have a lower risk of heart disease, type 2 diabetes and obesity.", Toast.LENGTH_LONG);
myToast.show();
}
else if (randomNum == 10) {
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, "A high protein intake can boost metabolism significantly, while making you feel so full that you automatically eat fewer calories.", Toast.LENGTH_LONG);
myToast.show();
}
else {
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, "Extra virgin olive oil is the healthiest fat on the planet.", Toast.LENGTH_LONG);
myToast.show();
}
}
public void imageClick(View view) {
//Picks a random number a then applys a random image depending on that number
Random rand = new Random();
int randomImage = rand.nextInt((4 - 1) + 1) + 1;
if (randomImage == 1) {
ImageButton image = findViewById(R.id.imageView3);
image.setImageResource(R.drawable.fitness);
}
else if (randomImage == 2) {
ImageButton image = findViewById(R.id.imageView3);
image.setImageResource(R.drawable.fitness2);
}
else if (randomImage == 3) {
ImageButton image = findViewById(R.id.imageView3);
image.setImageResource(R.drawable.fitness3);
}
else {
ImageButton image = findViewById(R.id.imageView3);
image.setImageResource(R.drawable.fitness4);
}
}
// Code for when a user has entered a stone value and clicks the button to convert to KG.
public void convertStoneClick(View view) {
// Play a pop noise to signal button has been clicked
final MediaPlayer mp = MediaPlayer.create(this, R.raw.pop);
mp.start();
// Closes the keyboard after a user has clicked the convert button
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
// Changes the picture at the top of the screen
//Picks a random number a then applies a random image depending on that number
Random rand = new Random();
int randomImage = rand.nextInt((4 - 1) + 1) + 1;
if (randomImage == 1) {
ImageButton image = findViewById(R.id.imageView3);
image.setImageResource(R.drawable.fitness);
}
else if (randomImage == 2) {
ImageButton image = findViewById(R.id.imageView3);
image.setImageResource(R.drawable.fitness2);
}
else if (randomImage == 3) {
ImageButton image = findViewById(R.id.imageView3);
image.setImageResource(R.drawable.fitness3);
}
else {
ImageButton image = findViewById(R.id.imageView3);
image.setImageResource(R.drawable.fitness4);
}
// Initialize stoneText variable with the amount the user has entered as their stone value.
EditText stoneText = findViewById(R.id.stoneText);
// Converts stones to KG, or brings up a error toast for the user if they did not enter numbers.
try {
String amountInStone = stoneText.getText().toString();
double amountInKgDouble = Double.parseDouble(amountInStone) * 6.35029;
String amountinKgString = String.format(Locale.UK,"%.1f", amountInKgDouble);
// Pops up a toast displaying the conversion.
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, amountInStone + " stone, is " + amountinKgString + " KG", Toast.LENGTH_LONG);
myToast.show();
// Deletes the text from the editText so the user can more efficiently enter a new value.
//stoneText.getText().clear();
} catch (NumberFormatException e) {
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, "Please enter numbers only!", Toast.LENGTH_LONG);
myToast.show();
stoneText.getText().clear();
}
}
// Code for when a user has entered a kg value and clicks the button to convert to stone.
public void convertKgClick(View view) {
// Play a pop noise to signal button has been clicked
final MediaPlayer mp = MediaPlayer.create(this, R.raw.pop);
mp.start();
// Closes the keyboard after a user has clicked the convert button
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
// Changes the picture at the top of the screen
//Picks a random number a then applys a random image depending on that number
Random rand = new Random();
int randomImage = rand.nextInt((4 - 1) + 1) + 1;
if (randomImage == 1) {
ImageButton image = findViewById(R.id.imageView3);
image.setImageResource(R.drawable.fitness);
}
else if (randomImage == 2) {
ImageButton image = findViewById(R.id.imageView3);
image.setImageResource(R.drawable.fitness2);
}
else if (randomImage == 3) {
ImageButton image = findViewById(R.id.imageView3);
image.setImageResource(R.drawable.fitness3);
}
else {
ImageButton image = findViewById(R.id.imageView3);
image.setImageResource(R.drawable.fitness4);
}
// Initialize kgText variable with the amount the user has entered as their KG value.
EditText kgText = findViewById(R.id.KgText);
// Converts KG to stone, or brings up a error toast for the user if they did not enter numbers.
try {
String amountInKg = kgText.getText().toString();
double amountInStoneDouble = Double.parseDouble(amountInKg) / 6.35029;
String amountinStoneString = String.format(Locale.UK,"%.1f", amountInStoneDouble);
// Pops up a toast displaying the conversion result.
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, amountInKg + " KG, is " + amountinStoneString + " Stone", Toast.LENGTH_LONG);
myToast.show();
// Deletes the editText so the user can more efficiently enter a new value.
//kgText.getText().clear();
} catch (NumberFormatException e) {
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, "Please enter numbers only!", Toast.LENGTH_LONG);
myToast.show();
kgText.getText().clear();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Ensures when the user first starts the app that the keyboard will not pop up.
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
// Initialize and load an advert
AdView mAdView;
MobileAds.initialize(this, "ca-app-pub-2327915784489185~3045931288");
// Test ad unit (Use when testing app)
// ca-app-pub-3940256099942544/6300978111
// Real ad unit (Use for release)
// ca-app-pub-2327915784489185/1078421100
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Opening welcome Toast for the user.
if (myToast != null) {
myToast.cancel();
}
myToast = Toast.makeText(this, "Welcome to the converter", Toast.LENGTH_SHORT);
myToast.show();
}
@Override
protected void onDestroy() {
// If app is destroyed any toasts will be stop displaying.
myToast.cancel();
super.onDestroy();
}
@Override
protected void onStop () {
// If app is stopped any toasts will be stop displaying.
super.onStop();
myToast.cancel();
}
}
因此,启动应用程序时似乎发生了错误。但并非适用于所有用户。我已经有其他人对其进行了测试,并且效果很好,但是一些用户无法打开该应用。
很抱歉,如果我的格式不完美,我对编码以及stackoverflow还是陌生的。
我本人似乎无法产生此错误,但是从Google Play商店崩溃报告中可以看出来。
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3349)
at android.app.ActivityThread.access$1100 (ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1794)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:158)
at android.app.ActivityThread.main (ActivityThread.java:7224)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)
Caused by: android.view.InflateException:
at android.view.LayoutInflater.inflate (LayoutInflater.java:551)
at android.view.LayoutInflater.inflate (LayoutInflater.java:429)
at android.view.LayoutInflater.inflate (LayoutInflater.java:380)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView (AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView (AppCompatActivity.java:139)
at keithcolclough.stonestokilograms.stonetokilogramsconverter.MainActivity.onCreate (MainActivity.java:268)
at android.app.Activity.performCreate (Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3206)
Caused by: android.view.InflateException:
at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:794)
at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:716)
at android.view.LayoutInflater.rInflate (LayoutInflater.java:847)
at android.view.LayoutInflater.rInflateChildren (LayoutInflater.java:810)
at android.view.LayoutInflater.rInflate (LayoutInflater.java:855)
at android.view.LayoutInflater.rInflateChildren (LayoutInflater.java:810)
at android.view.LayoutInflater.inflate (LayoutInflater.java:527)
Caused by: android.content.res.Resources$NotFoundException:
at android.content.res.Resources.getValue (Resources.java:2558)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates (AppCompatDrawableManager.java:328)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable (AppCompatDrawableManager.java:193)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable (AppCompatDrawableManager.java:186)
at android.support.v7.content.res.AppCompatResources.getDrawable (AppCompatResources.java:100)
at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes (AppCompatImageHelper.java:58)
at android.support.v7.widget.AppCompatImageButton.<init> (AppCompatImageButton.java:78)
at android.support.v7.widget.AppCompatImageButton.<init> (AppCompatImageButton.java:68)
at android.support.v7.app.AppCompatViewInflater.createImageButton (AppCompatViewInflater.java:201)
at android.support.v7.app.AppCompatViewInflater.createView (AppCompatViewInflater.java:121)
at android.support.v7.app.AppCompatDelegateImplV9.createView (AppCompatDelegateImplV9.java:1035)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView (AppCompatDelegateImplV9.java:1092)
at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:758)
答案 0 :(得分:0)
我想我已经解决了!
我终于设法使错误对我出现,并且解决该问题的方法是重新导入所有可绘制图像并将它们放置在常规可绘制文件夹中,而不是v24文件夹中。
现在运行平稳。