Bandit013
Bandit013

Reputation: 1

App keeps stopping - fails to launch

Uber noob to programming but have started trying to make a really basic app using a tutorial (https://developer.android.com/training/basics/firstapp/starting-activity.html) but I've run into a few road blocks. I've tried making this app twice and failed miserably. One issue is that the viewGroup line keeps disappearing from the code as soon as its entered. The second issue is that the app crashes as soon as it starts. I had the first activity working initially but the app would crash when the button was clicked. I've triad clean/rebuild but it hasn't helped at all. I've also tried other basic info I could like editing or removing certain parts of the code but they didn't help either. So at this stage any and all help would be greatly appreciated! Thanks in advance!

Logcat error info

 06-14 20:56:40.335 25791-25791/com.example.jacqueline.myfirstapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                       Process: com.example.jacqueline.myfirstapp, PID: 25791
                                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jacqueline.myfirstapp/com.example.jacqueline.myfirstapp.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2711)
                                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2772)
                                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                           at android.os.Looper.loop(Looper.java:241)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6217)
                                                                                           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: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
                                                                                           at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:359)
                                                                                           at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:328)
                                                                                           at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
                                                                                           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                                           at com.example.jacqueline.myfirstapp.MainActivity.onCreate(MainActivity.java:15)
                                                                                           at android.app.Activity.performCreate(Activity.java:6705)
                                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2664)
                                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2772) 
                                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1515) 
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                           at android.os.Looper.loop(Looper.java:241) 
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6217) 
                                                                                           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)  

MAIN ACTIVITY -

package com.example.jacqueline.myfirstapp;

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


public class MainActivity extends AppCompatActivity {
    public static final String EXTRA_MESSAGE = "com.example.jacqueline.myfirstapp.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    /**called when user taps the send button*/
    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.editText);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
    }

    public class EXTRA_MESSAGE {
    }
}  

Upvotes: 0

Views: 705

Answers (2)

JSharma
JSharma

Reputation: 113

You need to mention Theme.AppCompat theme (or descendant) with this activity.

  1. first of all open your manifest file from project explorer window.
  2. add theme to your activity.

    <application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:roundIcon="@mipmap/ic_launcher_round"
       android:supportsRtl="true"
       android:theme="@style/AppTheme">
                <activity android:name=".MainActivity"
               android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
                        <intent-filter>
                                    <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    

Upvotes: 0

Darish
Darish

Reputation: 11481

The stack trace clearly state that

You need to use a Theme.AppCompat theme (or descendant) with this activity

In your manifest file, declare Theme.AppCompat.NoActionBar as your theme`

<activity android:name=".MainActivity"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>`

Upvotes: 1

Related Questions