简体   繁体   中英

Android application error (Activity Main)

I am facing some problem when I want to run my application (debug) from my smartphone. Here's the error.

09-16 19:18:32.703 15432-15432/com.myApp.Name E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myApp.Name, PID: 15432
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myApp.Name/com.myApp.Name.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2466)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2528)
    at android.app.ActivityThread.access$800(ActivityThread.java:169)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5552)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
    at android.view.LayoutInflater.createView(LayoutInflater.java:637)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:486)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:400)
    at android.app.Activity.setContentView(Activity.java:2168)
    at com.myApp.Name.MainActivity.onCreate(MainActivity.java:15)
    at android.app.Activity.performCreate(Activity.java:5975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2528) 
    at android.app.ActivityThread.access$800(ActivityThread.java:169) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:194) 
    at android.app.ActivityThread.main(ActivityThread.java:5552) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) 
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
    at android.view.LayoutInflater.createView(LayoutInflater.java:611)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:486) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:418) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:400) 
    at android.app.Activity.setContentView(Activity.java:2168) 
    at com.myApp.Name.MainActivity.onCreate(MainActivity.java:15) 
    at android.app.Activity.performCreate(Activity.java:5975) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2528) 
    at android.app.ActivityThread.access$800(ActivityThread.java:169) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:194) 
    at android.app.ActivityThread.main(ActivityThread.java:5552) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) 
Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
    at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:34)
    at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:182)
    at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:176)
    at java.lang.reflect.Constructor.newInstance(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:611) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:486) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:418) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:400) 
    at android.app.Activity.setContentView(Activity.java:2168) 
    at com.myApp.Name.MainActivity.onCreate(MainActivity.java:15) 
    at android.app.Activity.performCreate(Activity.java:5975) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2528) 
    at android.app.ActivityThread.access$800(ActivityThread.java:169) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:194) 
    at android.app.ActivityThread.main(ActivityThread.java:5552) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) 
09-16 19:18:36.755 15432-15432/com.myApp.Name I/Process: Sending signal. PID: 15432 SIG: 9

And the code named MainActivity.java :

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Set UI layout as the view
        setContentView(R.layout.activity_main);

        //Get a reference to the button in our layout
        final Button buttonPlay = (Button)findViewById(R.id.buttonPlay);
        //Listen for clicks
       buttonPlay.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //Must be the play button.
        //Create a new Intent object
        Intent i = new Intent(this, GameActivity.class);
        //Start our GameActivity class via the Intent
        startActivity(i);
        // Now shut this activity down
        finish();
    }
}

How do I solve the problem?

After I commented this line of code:

setContentView(R.layout.activity_main);

It showed another error on this line:

buttonPlay.setOnClickListener(this);

If I comment that 2 line of codes, the application runs, but only blank screen appears.

activity_main.xml :

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

content_main.xml code :

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.gamecodeschool.c1tappydefender.MainActivity"
tools:showIn="@layout/activity_main"
android:background="@drawable/background">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id="@+id/textHighScore"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/buttonPlay"
    android:layout_marginBottom="60dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="60dp"
    android:layout_marginStart="60dp" />

Here it seems that your theme is cause this error:

You need to use a Theme.AppCompat theme (or descendant) with the design library. .

Try using another theme, Theme.AppCompat.Light or whatever.

Edit: Try this

<style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
</style>

您是否在build.gradle文件中添加了类似的内容

compile 'com.android.support:appcompat-v7:23.1.1'
android:theme="@style/Theme.AppCompat"

将此添加到xml文件中或检查样式文件和清单文件中您的应用主题是什么?

It's look like a found the problem. I added android:theme="@android:style/Theme.NoTitleBar.Fullscreen" to AndroidManifest.xml following the manual book. When i remove it, application run well, but not fullscreen. I want to use that theme, so what should i do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM