Hey, I realize there are tutorials on this topic, and even previous questions posed. However, I've read several tutorials and some answers and I am still having trouble. Clearly, I must not be the brightest crayon in the box.
My program crashes when I try to switch between activities with the following code:
final Button switchButton = (Button) findViewById(R.id.change_mode);
switchButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent runOptionSelect = new Intent(TheDecider.this, OptionSelect.class);
startActivity(runOptionSelect);
return;
}
});
I think this code is fine so it must be an issue with the manifest.xml right? I don't understand when to use which activity constant. If my purpose is simply to switch to a different layout and class, what should I choose?
Also, are MAIN and LAUNCHER only to be used on the initial activity to be run?
So sorry for asking such a basic question but I've spent far too much time researching this to no avail. Thank you.
Intents: Navigating between Activities (Amongst Other Things). We mentioned earlier that navigation between activities is managed by Intents. An Intent is a type of message that applications broadcast through the Android OS to interested parties on the phone.
Please check below code in your manifest.xml file
<activity android:name=".TheDecider"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".OptionSelect"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
In the intent, the first parameter is the current context (you can do
TheDecider.this
or
getApplicationContext()
there) and the second one is the class from the activity you are trying to reach.
You are doing that right. And in your manifest you should add
<activity android:name=".OptionSelect"
android:label="@string/app_name" />
You have to add EVERY Activity in your Manifest, otherwise it will crash. Without knowing your logcat's content, that's all i can say.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With