9

I have started an Intent and asked it to go to the main activity, when it attempts it the app crashes.

Here is the code that tries to go to the main activity.

Intent i = new Intent(
".MAIN_ACTIVITY");
startActivity(i);   

Here is the XML manifest for Main_Activity.

<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN_ACTIVITY" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

I'm still pretty new to this so any help and/or advice is of great value.

Christian Gardner
  • 237
  • 2
  • 3
  • 6

4 Answers4

26

Write like this :

Intent i = new Intent(MainActivity.this, NewActivity.class);
startActivity(i);

Also you need to declare both activity class in manifest file like this:

<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
    <action android:name="android.intent.action.MAIN_ACTIVITY" />

    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
    android:name=".NewActivity"
    android:label="@string/app_name" >
</activity>
krishna
  • 978
  • 1
  • 8
  • 14
4

For those, who come from Google, I was trying to pass large string in putExtra (over 90K Symbols) and my app was crashing because of that. The correct solution is either save the string to file or implement Singleton.

Here is the relevant link Maximum length of Intent putExtra method? (Force close)

Community
  • 1
  • 1
Andrew Marin
  • 1,053
  • 1
  • 13
  • 15
  • I've stumbled on your post through Google after experiencing the same sort of crash. Apparently in my case the crash was caused by `NewActivity` inheriting `AppCompatActivity` (added by default by Android Studio when creating a new Activity) while `MainActivity` inherited `Activity`. Once I've switched `AppCompatActivity` with `Activity` the app worked as expected. – RAM Jul 28 '18 at 08:41
2

as per your code: if i have create newActiviy in my project then:

i have to add that activity in android manifest file.

like:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN_ACTIVITY" />

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <activity android:name=".newActivity"></activity>
</activity>

for calling that activity just do:

Intent intent = new Intent(MainActivity.this, newActivity.class);
    startActivity(intent);

befre ask question here try some googling. and you must have to check this: Building Your First Android App and Starting Another Activity

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0

Start new Activity like this:

Intent intent = new Intent(YourCurrentActivity.this, TargetActivity.class);
    startActivity(intent);
Shiv
  • 4,569
  • 4
  • 25
  • 39