Android Studio始终打开相同的错误页面

时间:2014-12-08 02:45:06

标签: java android eclipse onclicklistener start-activity

我无法弄清楚这一点。无论我点击什么,我的应用程序总是会转到.Weather类。

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;
        }
}

尝试访问mapview类

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);
    }

}

我不知道问题是什么,因为我之前做过同样的事情并且它完美地工作了......

2 个答案:

答案 0 :(得分:1)

你错过了break s:

@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;
    }
}

答案 1 :(得分:1)

您应该在每个案例陈述中添加中断

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;