Android app closes when attempting to open certain activities - java

Immediate Disclaimer: I am not a programmer, I've been dumped with this as part of a group project, so apologies if the code is shabby.
I've got a main activity as the start-up page with several buttons that should open different activities, three of these buttons work perfectly with opening up their specific activities (Main2Activity, MOT and Garage), but the others, with the same structure being used, just close the app instead of opening the next screen.
public void defineButtons() {
findViewById(R.id.mot_button).setOnClickListener(buttonClickListener);
findViewById(R.id.enter_button).setOnClickListener(buttonClickListener);
findViewById(R.id.garage_button).setOnClickListener(buttonClickListener);
findViewById(R.id.profile_button).setOnClickListener(buttonClickListener);
findViewById(R.id.contact_button).setOnClickListener(buttonClickListener);
findViewById(R.id.settings_button).setOnClickListener(buttonClickListener);
}
private View.OnClickListener buttonClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mot_button:
Intent intent = new Intent(MainActivity.this, MOT.class);
startActivity(intent);
break;
case R.id.garage_button:
Intent x = new Intent(MainActivity.this, garage.class);
startActivity(x);
break;
case R.id.profile_button:
Intent a = new Intent(MainActivity.this, Profile.class);
startActivity(a);
break;
case R.id.contact_button:
Intent b = new Intent(MainActivity.this, Contact.class);
startActivity(b);
break;
case R.id.settings_button:
Intent c = new Intent(MainActivity.this, Activity_Settings.class);
startActivity(c);
break;
case R.id.enter_button:
reg_input=findViewById(R.id.reg_input);
Intent i = new Intent(MainActivity.this, Main2Activity.class);
regNo = reg_input.getText().toString();
i.putExtra("Value", regNo);
startActivity(i);
finish();
break;
This is the relevant code for it, let me know if you want to see anything else.
I'm probably being really stupid, but I'd appreciate the help.

Your app is crashing, probably because the activities your want to start aren't in the manifest. Check your manifest and make sure that all your activities are declared there.

Related

App crashes when switching from activity 1 to activity 2

In my app, there are so many activities which are referred to as levels. And one activity is Reward activity. when i win level-1, reward activity opens. Now i want to replay the level-1. For this i have used getExtra(). My app crashes when i click the replay button.
Houselevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.HouseLevel2");
startActivity(intent);
}
if(activity.equals("level1")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Houselevel1");
startActivity(intent);
}
}
});
}
With the java code you've posted, in the Reward.java file, you're trying to create another Intent Object with the same name as the one declared in the scope right above it. Because of this, the build will never be successful.
Also, when you declare intents, you MUST pass on the activity_name.class file.
Something you can try:
1) HouseLevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
2) HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
3) Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.HouseLevel2.class);
startActivity(intent);
}
else if(activity.equals("level1")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Houselevel1.class);
startActivity(intent);
}
}
});
}
Also, if you're simply using the Reward.java file to get the previous intent's data, perform some calculation, and send some data back to the calling, or parent activity, then you can simply use the startActivityForResult() method, which takes care what what you're trying to do manually.
Here's a small article that might be able to help you with the problem
http://www.vogella.com/tutorials/AndroidIntent/article.html#retrieving-result-data-from-a-sub-activity
If all you want is go from Activity 1 or to 2 to a Reward activity grab something and send that something back to either activity.
What you do is startActivityForResult You pass an Id (constant number) do what you do on the Reward activty, pack what you need to return in a Bundle, and set ActivtyResult to OK and close your activity.
Your app will go back to the Activity1 or 2 whoever call it. On those activties you override the method onActivityResult There you check if the id on which the result is coming from is the Id you sent on the startActivityForResult and if the status is OK.
Then you have whatever was set on the Reward activity. The Reward activity don't need to know from where it came from if only will grab some data. So you can later have an Activity3 that calls the Reward activity and you do not need to modify the Reward activity.
It is explain here check the accepted answer.
How to manage `startActivityForResult` on Android?

How can I launch a new Activity to a Fragment that is not the initial fragment?

How can I launch a new Activity to a Fragment that is not the initial fragment? For example, the following code is wrong. I want to launch the MainActivity.class AT the SecondFragment.class. Seems simple enough but cannot find an answer anywhere. All help is greatly appreciated!
public void LaunchSecondFragment(View view) {
view.startAnimation(AnimationUtils.loadAnimation(this, R.anim.image_click));
Intent intent = new Intent(this, SecondFragment.class);
startActivity(intent);
}
So, before starting an activity you have to do something like:
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("launchSecondFragment", true)
startActivity(intent)
and in your MainActivity onCreate()
if(getIntent().getBooleanExtra("launchSecondFragment", false)) {
//do fragment transaction to second fragment
} else {
//do fragment transaction to the first fragment
}
UPDATE
So, here is the clever way to do it.
First of all create enum in your MainActivity.class
public enum FragmentNames {
FIRST_FRAGMENT,
SECOND_FRAGMENT
}
then define a string constant for getting and putting this extra(also in MainActivity)
public static final String FRAGMENT_EXTRA = "fragmentExtra";
So now when you start an activity you should do it like this:
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(MainActivity.FRAGMENT_EXTRA, MainActivity.FragmentNames.SECOND_FRAGMENT);
startActivity(intent);
And catch in your MainActivity onCreate() method:
FragmentNames name = getIntent().getSerializableExtra(FRAGMENT_EXTRA);
switch(name) {
case FIRST_FRAGMENT:
//do stuff
break;
case SECOND_FRAGMENT:
//do stuff
break;
default:
//load default fragment(FirstFragment for example)
}
What else is cool about enums? You mentioned that you are using this intents to define current item of your ViewPager. Well, good news, enums have ordinal().
Basically you can do something like:
mViewPager.setCurrentItem(name.ordinal());
In this case ordinal() of the FIRST_FRAGMENT is 0 and ordinal of SECOND_FRAGMENT is 1.
Just don't forget to check for nulls :)
Cheers.
Try this to start the activity:
Intent intent = new Intent(this, MainActivity.class);
int fragmentIndex = 2;
intent.putExtra("fragment_index", fragmentIndex);
startActivity(intent);
and this for the MainActivity's onCreate
Bundle extras = getIntent().getExtras();
int fragmentIndex;
if(extras != null) {
fragmentIndex = extras.getInt("fragment_index",1);
}
switch(fragmentIndex) {
case 1:
//display fragment 1
break;
case 2:
//display fragment 2
break;
case 3:
//display fragment 3
break;
}
When user clicks button and your MainActivity opens, its onCreate() will be get called.
You should add fragment transaction in onCreate() to launch SecondFragment :
FragmentTransaction ft = getFragmentManager().beginTransaction();
SecondFragment secondFragment = new SecondFragment();
ft.replace(R.id.content_frame, secondFragment);
ft.commitAllowingStateLoss();

Error when starting new activity from menu click

I am new to android development so there is probably something simple that is wrong. If you need any more info I will be glad to give that to you. Thanks in advance.
I am trying to add a button in my navdrawer.class. This is what I have.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.new_account:
Intent intent = new Intent(this, AddAccountActivity.class);
this.startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
}
I get an error.
since you´re into a fragment you must use:
Intent intent = new Intent(getActivity(), AddAccountActivity.class);
or
Intent intent = new Intent(getActivity().getApplicationContext(), AddAccountActivity.class);
for example see the context used in your Toast (getActivity())
Toast.makeText(getActivity(), "This Will Create A New Account.", Toast.LENGTH_SHORT).show();
You should write
Intent intent = new Intent(AddAccountActivity.this, AddAccountActivity.class);
instead of
Intent intent = new Intent(this, AddAccountActivity.class);
Am I right that this is the fragment instance? If this is the case, thats your problem. The intent constructor needs a context and an activity class to work.
Fragment does not inherit from context. You can get the underlying activity with the getActivity() method.
try this:
Intent intent = new Intent(getActivity(), AddAccountActivity.class);

How does radio button stay selected when other activity is initiated using an intent?

My Main activity has a group radio buttons. When each of them is clicked, it initiates a different activity. All the activities have the same XML but the inputs and the functionality is different. What I mean is the radio group is common for all Activities. For Example: When a first radio button is clicked, it initiates a different activity and the radio box selection disappears. I need the clicked radio button selection to stay on until a different radio button from the group is clicked though it is in a different activity. How do I get this working? Any help will be greatly appreciated!
public void onRadioButtonClicked(View view){
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_199os:
if (checked){
Intent intent = new Intent(MainActivity.this, second.class);
startActivity(intent);
}
break;
case R.id.radio_399os:
if (checked){
Intent intent = new Intent(MainActivity.this, Third.class);
startActivity(intent);
}
break;
case R.id.radio_2000os:
if (checked){
Intent intent = new Intent(MainActivity.this, Fourth.class);
startActivity(intent);
}
//
break;
}
}
you need to add the following to make checked
check for selected
radio_2000os.setChecked(true);
uncheck for others
radio_199os.setChecked(false);
your Code would be like this
public void onRadioButtonClicked(View view){
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_199os:
if (checked){
Intent intent = new Intent(MainActivity.this, second.class);
startActivity(intent);
radio_2000os.setChecked(false);
radio_399os.setChecked(false);
radio_199os.setChecked(true);
}
break;
case R.id.radio_399os:
if (checked){
Intent intent = new Intent(MainActivity.this, Third.class);
startActivity(intent);
radio_2000os.setChecked(false);
radio_399os.setChecked(true);
radio_199os.setChecked(false);
}
break;
case R.id.radio_2000os:
if (checked){
Intent intent = new Intent(MainActivity.this, Fourth.class);
startActivity(intent);
radio_2000os.setChecked(true);
radio_399os.setChecked(false);
radio_199os.setChecked(false);
}
//
break;
}
}

case/switch statement

i need some help with switch/case statement syntax
im trying to use onClick to have different buttons do different things
i have the first one working
it just uses an intent to start a new activity.
for the next button, i want it to open a specific url
looks like this so far
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
case R.id.engadget_button:
Intent a = new Intent(this, )
how do i get the engadget_button to open engadget.com?
Is it just something like this:
case R.id.engadget_button:
String url ="http://www.engadget.com/";
Intent a = new Intent(Intent.ACTION_VIEW);
a.setData(Uri.parse(url));
startActivity(a);
break;
??
http://www.androidsoftwaredeveloper.com/2009/04/15/how-to-open-a-url-from-code/

Categories

Resources