java - 具有不同布局的多个按钮

标签 java android button layout android-activity

我的一项 Activity 有 5 个按钮。每个按钮应显示一项 Activity (总共 5 种不同的 Activity )。我尝试了多种方法来处理多个按钮,但我的应用程序不起作用。 Activity 的第一个按钮启动其链接的 Activity 没有任何问题(btEtiologia, Activity 是 Etiology),但第二个按钮仍然不会显示,并返回到主 Activity (即主页布局)。当我重新启动应用程序时,我尝试使用第二个按钮和第三个按钮并工作,但第四个按钮不会显示并返回到主要 Activity 。我已经在三星 Galaxy S2 上下载了该应用程序,它与模拟器相同。我的代码有什么问题吗?

这是Inicio的java代码:

package com.example.protesis;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Inicio extends ActionBarActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_inicio);
    Button btInformacion = (Button) findViewById(R.id.btInformacion);
    btInformacion.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Toast.makeText(getBaseContext(), "Esta aplicación esta basada en el manejo de las infecciones de prótesis articulares de la IDSA", Toast.LENGTH_SHORT).show();
        }
    });

        findViewById(R.id.btEtiologia).setOnClickListener(this);
        findViewById(R.id.btAlgoritmo).setOnClickListener(this);
        findViewById(R.id.btAntibioiv).setOnClickListener(this);
        findViewById(R.id.btAntibioral).setOnClickListener(this);
        findViewById(R.id.btRetirada).setOnClickListener(this);

    }

    public void onClick(View arg0){

        // create a general intent
        Intent intent = null;
        // define an intent for all cases
        switch(arg0.getId()){
            case R.id.btEtiologia:
                // Setting intent for first button
                intent = new Intent(this,Etiology.class);
                break;
            case R.id.btAlgoritmo:
                // Setting intent for second button
                intent = new Intent(this,Algoritmo.class);
                break;
            case R.id.btAntibioiv:
                // Setting intent for third button
                intent = new Intent(this,Antibioiv.class);
                break;
            case R.id.btAntibioral:
                // Setting intent for fourth button
                intent = new Intent(this,Antibioral.class);
                break;
            case R.id.btRetirada:
                // Setting intent for fourth button
                intent = new Intent(this,Retirada.class);
                break;              
}         
this.startActivity(intent); 
                    }           
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.inicio, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

主要 Activity 代码是:

package com.example.protesis;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btInicio = (Button) findViewById(R.id.btInicio);
    btInicio.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), Inicio.class);
                startActivityForResult(intent, 0);
        }
        });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

最佳答案

根据您的代码,您的图像将代码拖动到 OutOfMemory 异常。虽然您使用的图像为 120 dpi,但它们的尺寸是 640*480 的两倍。请更改图像尺寸,然后问题就会解决。我已经更改了所有 5 个图像尺寸,现在我可以通过单击按钮导航到所有屏幕。将ldpi文件夹中的图像调整为640*480左右。

关于java - 具有不同布局的多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25766991/

相关文章:

java - Oracle JVM 运行时错误 : Provider com. sun.xml.stream.ZephyrParserFactory 未找到

java - 将 2 个 txt 文件中的一些元素添加到第三个文件中

java - 与谷歌整合加上 : initializing google plus API client client is not working

Android按钮选择并按下drawable

javascript - onclick 按钮获取事件图像信息

java - 无法通过 getMethod() 找到公共(public)方法

java - 为什么当我想从文本文件中删除字符串时它不起作用?

android - 使用没有 mediaextractor 的 mediacodec 的 MP3 解码失败

android - 如何使 TextView 的每个单词都可点击?

javascript - 如何激活:focus on the input+button when a user click into the input?