【问题标题】:Android Studio always opens same incorrect pageAndroid Studio 总是打开相同的错误页面
【发布时间】:2014-12-08 02:45:05
【问题描述】:

我想不通。无论我点击什么,我的应用都将始终转到 .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;
        }
}

试图进入地图视图类

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

}

我不知道问题出在哪里,因为我以前做过同样的事情,而且效果很好......

【问题讨论】:

    标签: java android eclipse onclicklistener start-activity


    【解决方案1】:

    你应该在每个case语句中添加break

    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;
    

    【讨论】:

      【解决方案2】:

      你错过了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;
          }
      }
      

      【讨论】:

      • 发生在我们最好的人身上^^
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      相关资源
      最近更新 更多