有一个FATAL EXCEPTION:在我的项目中,MainActivity无效

时间:2018-04-11 22:49:32

标签: java android fatal-error

MainActivity无效。 我试图在模拟器上运行它,但应用程序被迫停止。 我试图从MainActivity跳转到UserActivity

MainActivity.java

package com.example.kanishk.coachingfinder;

    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button clickHere = (Button)findViewById(R.id.clickHere);
    clickHere.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent mainIntent = new Intent(MainActivity.this,UserActivity.class);
            startActivity(mainIntent);
        }
    });
}
}

LogCat(参数在许多代码行的下一行)

 04-12 03:44:51.709 4607-4607/com.example.kanishk.coachingfinder 
E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.kanishk.coachingfinder, PID: 4607
java.lang.RuntimeException: Unable to start activity 
 ComponentInfo{com.example.kanishk.coachingfinder/
com.example.kanishk.coachingfinder.MainActivity}: 
android.view.InflateException: Binary XML file line #10: Error inflating 
class ImageView
    at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
    at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)

下面是activity_main.xml文件。我无法找到问题所在。

<?xml version="1.0" encoding="utf-8"?>
<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:background="#ccd9ff"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.kanishk.coachingfinder.MainActivity">

<ImageView
    android:layout_width="385dp"
    android:layout_height="305dp"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:scaleType="centerCrop"
    android:src="@mipmap/cfinder"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.888"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView3"
    app:layout_constraintVertical_bias="0.161" />



<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Welcome to"
    android:textAlignment="center"
    android:textAppearance="@style/TextAppearance.AppCompat"
    android:textColor="#91b4ff"
    android:textSize="35sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    tools:ignore="MissingConstraints" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="364dp"
    android:layout_height="96dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="COACHING FINDER"
    android:textAlignment="center"
    android:textColor="#ff7f50"
    android:textSize="40sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView2"
    app:layout_constraintVertical_bias="0.0"
    tools:ignore="MissingConstraints" />

<Button
    android:id="@+id/clickHere"
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="79dp"
    android:layout_height="55dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="132dp"
    android:text="CLICK HERE"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.427"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView3"
    app:layout_constraintVertical_bias="0.018" />


</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:1)

我怀疑这是由于XML布局中的配置问题造成的。这个问题有一个类似的例外:

InflateException: Binary XML file line #8: Error inflating class ImageView

我会检查您的图片来源,是否存在于正确的位置以及文件格式是什么。

同样发布XML将有助于调试。

答案 1 :(得分:0)

虽然我不完全确定您要做的是什么,但请尝试这样做:

Intent mainIntent = new Intent(getApplicationContext(), UserActivity.class);

这是我的资源,因为我之前已经这样做了: https://www.youtube.com/watch?v=6ow3L39Wxmg

跳到大约21:45看一下类似的代码。