24

Newbie trying to finish the My first App tutorial provided by Google. On the way to this fatal exception I did import a lot of random packages to get rid of "cannot be resolved" errors for a number of things, ActionBarActivity, EditText, Fragment, LayoutInflater, etc, but not sure this matters. Anyway, my app crashes and produces a Fatal exception when I click on the "Send" button in the Main Activity. Here is my code and logcat file.

MyActivity.java (aka MainActivity.java of the tutorial)

package magiccoupons.tutapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;


public class MyActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    // Do something in response to button
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}
}

DisplayMessageActivity.java:

package magiccoupons.tutapp;

import android.widget.*;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.*;
import android.support.v7.app.ActionBarActivity;
import android.app.Fragment;

public class DisplayMessageActivity extends ActionBarActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);

    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    setContentView(textView);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 *
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() { }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_display_message,
                container, false);
        return rootView;
    }
}
*/
}

build.gradle:

 apply plugin: 'com.android.application'

android {
    compileSdkVersion 'android-L'
    buildToolsVersion '20'

    defaultConfig {
        applicationId "magiccoupons.tutapp"
        minSdkVersion 20
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:20.0.0'
    //compile 'com.android.support:appcompat-v7:21.0.0-rc1'
}

activity_my.xml:

    <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:orientation="horizontal"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="magiccoupons.tutapp.MainActivity">

    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage" />

</LinearLayout>

activity_display_message.xml:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="magiccoupons.tutapp.DisplayMessageActivity">

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

and logcat:

08-21 11:24:36.088    3645-3645/magiccoupons.tutapp I/Process﹕ Sending signal. PID: 3645 SIG: 9
08-21 11:37:33.584    4149-4149/magiccoupons.tutapp W/Resources﹕ Preloaded drawable resource #0x1080093 (android:drawable/sym_def_app_icon) that varies with configuration!!
08-21 11:37:33.676    4149-4149/magiccoupons.tutapp I/am_on_resume_called﹕ [0,magiccoupons.tutapp.MyActivity]
08-21 11:37:33.905    4149-4149/magiccoupons.tutapp D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
08-21 11:37:38.139    4149-4149/magiccoupons.tutapp I/am_on_paused_called﹕ [0,magiccoupons.tutapp.MyActivity]
08-21 11:37:38.218    4149-4149/magiccoupons.tutapp I/Choreographer﹕ Skipped 75 frames!  The application may be doing too much work on its main thread.
08-21 11:37:38.356    4149-4149/magiccoupons.tutapp D/AndroidRuntime﹕ Shutting down VM
08-21 11:37:38.369    4149-4149/magiccoupons.tutapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: magiccoupons.tutapp, PID: 4149
    java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/appcompat/R$styleable;
            at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:106)
            at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
            at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99)
            at magiccoupons.tutapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:5720)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
            at android.app.ActivityThread.access$800(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5070)
            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:836)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.appcompat.R$styleable" on path: DexPathList[[zip file "/data/app/magiccoupons.tutapp-1.apk"],nativeLibraryDirectories=[/data/app-lib/magiccoupons.tutapp-1, /vendor/lib, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:106)
            at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
            at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99)
            at magiccoupons.tutapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:15)
            at android.app.Activity.performCreate(Activity.java:5720)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
            at android.app.ActivityThread.access$800(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5070)
            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:836)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
    Suppressed: java.lang.ClassNotFoundException: android.support.v7.appcompat.R$styleable
            at java.lang.Class.classForName(Native Method)
            at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
            at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 18 more
     Caused by: java.lang.NoClassDefFoundError: Class "Landroid/support/v7/appcompat/R$styleable;" not found
            ... 22 more

Sorry if I missed something and this question is completely unnecessary and/or a dupe. Thanks.

rds
  • 26,253
  • 19
  • 107
  • 134
howdoyouturnthison
  • 255
  • 1
  • 4
  • 7
  • 2
    Why is com.android.support:appcompat-v7 commented in build.gradle? You are using android.support.v7.app.ActionBarActivity so you should include support v7 library. – Anton Savin Aug 21 '14 at 15:48
  • It makes no difference apparently, the message I get when I uncomment it is "Using the appcompact library when minSdkVersion >= 14 is not necessary". Also by leaving it in I get an UNEXPECTED TOP-LEVEL EXCEPTION – howdoyouturnthison Aug 21 '14 at 15:54
  • But if your minSdkVersion is >= 14 then you don't have to use support library at all. Just use Activity instead. – Anton Savin Aug 21 '14 at 16:00
  • Yeah I returned to this and just started completely anew. Using Activity instead of ActionBarActivity as described in the tutorial worked first time. Thanks. – howdoyouturnthison Sep 02 '14 at 14:57

5 Answers5

7

Just
     Build -> Clean Project
and then
     Build -> Rebuild Project.
That's all.

ucMedia
  • 4,105
  • 4
  • 38
  • 46
5

You are getting that error because of the following reasons:

In your Gradle build file your app is targeting and compiling with the beta version of Android that is still in development with:

compileSdkVersion 'android-L'
buildToolsVersion '20'

as well as

minSdkVersion 20
targetSdkVersion 20

First thing to note is that this app will not run correctly (at this time) on any device without android-L flashed to it.

The real crux of your issue is in DisplayMessageActivity, it extends via inheritance [ActionBarActivity]:(https://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html) this is one of the support library classes for AppCompat.

To fix this, change your min SDK to 10 (or 14 which is ice cream sandwich), your max SDK to 19 (Kit Kat) and un-comment the appcompat-v7 library in your dependencies.

As a side note, when you declare widgets in their respective activities/fragments it's usually good practice to have their scope be outside of any methods:

EditText editText;
Button sendMessageButton;

// Then in your onCreate() method
editText = (EditText) findViewById(R.id.editText);
sendMessageButton = (Button) findViewById(R.id.sendMessageButton);

This helps reduce memory re-allocation and make your code a little more readable. Sometimes you may have to bend the rules a bit but this is the common practice.

Puneet Verma
  • 1,373
  • 2
  • 19
  • 23
Patty P
  • 351
  • 2
  • 12
  • 1
    Yeah I returned to this eventually and just started completely anew. Targeted KitKat instead of L like you said and used Activity instead of ActionBarActivity. Worked perfectly, thanks. – howdoyouturnthison Sep 02 '14 at 14:58
5

I deleted 'build' folder in "yourAppName\app" folder

and everything worked fine. If above solution didn't work, try this. build folder will be auto-generate when you build your project again.

Darshn
  • 1,512
  • 1
  • 23
  • 31
  • 1
    Its a know bug with Android Studio. Its better to use File->Invalidate Caches / Restart... – Alex Dec 10 '18 at 12:20
  • @Alex wasn't aware of that!! most of the time i deal with bug of Android Studio than Bug in my app !! lol – Darshn Dec 10 '18 at 14:39
3

In my case the issue occurred in Android Studio 4.0. I suppose there was something wrong with Gradle cache.

Unfortunately, neither Invalidate Сaches / Restart, nor Build -> Clean Project & Build -> Rebuild Project didn't help me, so I had to take drastic measures.

  1. Close Android Studio.

  2. Open Terminal and navigate to your project's root directory.

  3. Delete all Gradle modules' build directories, so all modules will be recompiled soon.

    find . -wholename "*/build" -exec rm -rf {} \;
    

    But if you have a single module, then just:

    rm -rf app/build
    
  4. Purge Gradle cache. By default, it's located in your home directory.

    rm -rf ~/.gradle
    
  5. Open Android Studio again. It will start to build the project. It will take a long time, since we have just deleted the cache, but the issue should be resolved.

Yamashiro Rion
  • 1,778
  • 1
  • 20
  • 31
  • Thanks for this answer. Invalidate Cache/Restart wasn't enough for me either. Clean ~/.gradle and then start Android Studio again fixed it for me. – Lucy Oct 02 '20 at 00:37
2

In my case I had a layout which was defined for 720p but not defined for default resolutions so it was crashing. Adding that layout file fixed this issue, logcat dont lie.

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154