user2802752
user2802752

Reputation:

Android Studio always opens same incorrect page

I cannot figure this out. My app will always go to the .Weather class no matter what I click on.

package com.example.matmap;

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.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class ResourcesPage extends Activity implements OnClickListener {

    LinearLayout display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resources_page);
    //display = (LinearLayout) findViewById(R.id.menuSweet);
    LinearLayout locate = (LinearLayout) findViewById(R.id.locate);
    LinearLayout ndsu = (LinearLayout) findViewById(R.id.ndsu);
    LinearLayout weather = (LinearLayout) findViewById(R.id.weather);
    LinearLayout contact = (LinearLayout) findViewById(R.id.contact);

    locate.setOnClickListener(this);
    ndsu.setOnClickListener(this);
    weather.setOnClickListener(this);
    contact.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    switch(v.getId()){
    case R.id.locate:
        Intent myIntent = new Intent(ResourcesPage.this, MapView.class);
        startActivity(myIntent);
    case R.id.ndsu:
        Intent myIntent2 = new Intent(getApplicationContext(), TimesTable.class);
        startActivity(myIntent2);
    case R.id.weather:
        Intent myIntent3 = new Intent(getApplicationContext(), Weather.class);
        startActivity(myIntent3);
    break;  }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.resources_page, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.menuHome){
            Intent i = new Intent(getApplicationContext(), ResourcesPage.class);
            startActivity(i); 
        }
        return true;
        }
}

Trying to get to the mapview class

package com.example.matmap;

import android.app.Activity;
import android.os.Bundle;

public class MapView extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mapview);
    }

}

I don't know what the problem is since I've done these same things before and it's worked perfectly...

Upvotes: 1

Views: 117

Answers (2)

Phenix_yu
Phenix_yu

Reputation: 304

You should add break in each case statement

switch(v.getId()){
    case R.id.locate:
        Intent myIntent = new Intent(ResourcesPage.this, MapView.class);
        startActivity(myIntent);
        break;
    case R.id.ndsu:
        Intent myIntent2 = new Intent(getApplicationContext(), TimesTable.class);
        startActivity(myIntent2);
       break;

Upvotes: 1

Eugen Pechanec
Eugen Pechanec

Reputation: 38223

You're missing breaks:

@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.locate:
        Intent myIntent = new Intent(ResourcesPage.this, MapView.class);
        startActivity(myIntent);
        break;
    case R.id.ndsu:
        Intent myIntent2 = new Intent(getApplicationContext(), TimesTable.class);
        startActivity(myIntent2);
        break;
    case R.id.weather:
        Intent myIntent3 = new Intent(getApplicationContext(), Weather.class);
        startActivity(myIntent3);
        break;
    }
}

Upvotes: 0

Related Questions