0

I am getting an error while trying to run my app in my emulator

I am following a tutorial that shows you how to make an app like WhatsApp but I am having some trouble. Whenever I try to run the app in my emulator I get this really long error that I can't seem to solve. The only thing I changed that might've been of importance was the file name. It was MainActivity.jk or tk I forgot. But I changed it to MainActivity.Java. But this is the error I keep getting.

04-19 13:30:10.485 3614-3614/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.chatster, PID: 3614 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.chatster/com.example.chatster.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.chatster.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.chatster-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.chatster-1, /vendor/lib, /system/lib]] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.chatster.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.chatster-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.chatster-1, /vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:497) at java.lang.ClassLoader.loadClass(ClassLoader.java:457) at android.app.Instrumentation.newActivity(Instrumentation.java:1061) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)  at android.app.ActivityThread.access$800(ActivityThread.java:135)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5017)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)  at dalvik.system.NativeStart.main(Native Method) 

MainActivity.Java

package com.example.chatster;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import java.util.Objects;

public class MainActivity extends AppCompatActivity {

protected Bundle savedInstanceState;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    this.savedInstanceState = savedInstanceState;

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar mToolBar = (Toolbar) findViewById(R.id.Main_page_toolbar);
    setSupportActionBar(mToolBar);
    Objects.requireNonNull(getSupportActionBar()).setTitle("Chatster");
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      package="com.example.chatster">

<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" tools:ignore="AllowBackup,GoogleAppIndexingWarning">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

</manifest>
ismail alaoui
  • 5,748
  • 2
  • 21
  • 38
  • 1
    Possible duplicate of [java.lang.RuntimeException: Unable to instantiate activity ComponentInfo](https://stackoverflow.com/questions/4688277/java-lang-runtimeexception-unable-to-instantiate-activity-componentinfo) – user1506104 Apr 19 '19 at 19:19
  • Add Screenshot of your project structure, so that we can see in which package does your MainActivity exist. – TheAnkush Apr 19 '19 at 19:41
  • I can't because there is something wrong with my computer but it's under the android tab. app\java\chatster\com.example.chatster\MainActivity.Java –  Apr 19 '19 at 20:29
  • did you try putting in app/src/main/java/com/example/chatster/MainActivity.java? – user1506104 Apr 20 '19 at 06:40

2 Answers2

0

this seems to be problem in your case.: The relative path of your activity in manifest is not correct or your didn't declare your activity in your androidManifest

<activity android:name="com.example.chatster.MainActivity"

Please try to remvoe this line from your onCreate()

this.savedInstanceState = savedInstanceState ;

After you did this please clean and rebuild your project and restart your android studio

Please check this link for more information and hope you will resolve the problem

ismail alaoui
  • 5,748
  • 2
  • 21
  • 38
0

Disable instant run in Android Studio.

Go to File -> Settings/Preferences (Mac) -> Build Execution and Deployment -> Uncheck Instant Run

Then Clean and rebuild the project.

enter image description here

TheAnkush
  • 885
  • 7
  • 10