0

对于我的程序 Itax,除了 main.xml 之外,我还有几个布局。我在 main.xml 中有 3 个按钮和一个

  1. btn_info - 单击时打开 basicinfo.xml

  2. btn_tutorial - 单击时打开 tutorial.xml

  3. btn_calc - 点击时打开calculator.xml

我在 Main.java 中的这些按钮的代码是(这有效!):

package com.example.ITax;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MyActivity extends Activity {
    private Button btn1, btn2, btn3;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

         btn1 = (Button) findViewById(R.id.btn_info);
         btn2 = (Button) findViewById(R.id.btn_tutorial);
         btn3 = (Button) findViewById(R.id.btn_calc);

          btn1.setOnClickListener(i);
          btn2.setOnClickListener(i);
          btn3.setOnClickListener(i);


public View.OnClickListener i = new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if (v == btn1)
            {
                setContentView(R.layout.basic_info);
            }

            else if (v == btn2)
            {
                setContentView(R.layout.tutorial);
            }

            else if (v == btn3)
            {
                setContentView(R.layout.calcu);
            }


        }
    } ;    

但是..当我包含来自 calcu.xml 的按钮时,例如:

  1. btn_back_from_calcu - 单击时返回 main.xml

这是我的新代码不起作用(每当我在模拟器中运行我的应用程序时,它都会显示“不幸的是,ITax 停止了。”

package com.example.ITax;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MyActivity extends Activity {
    private Button btn1, btn2, btn3, btn4;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

         btn1 = (Button) findViewById(R.id.btn_info);
         btn2 = (Button) findViewById(R.id.btn_tutorial);
         btn3 = (Button) findViewById(R.id.btn_calc);
         btn4 = (Button) findViewById(R.id.btn_back_from_calcu);

          btn1.setOnClickListener(i);
          btn2.setOnClickListener(i);
          btn3.setOnClickListener(i);
          btn4.setOnClickListener(i);
}
}

public View.OnClickListener i = new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if (v == btn1)
            {
                setContentView(R.layout.basic_info);
            }

            else if (v == btn2)
            {
                setContentView(R.layout.tutorial);
            }

            else if (v == btn3)
            {
                setContentView(R.layout.calcu);
            }

             else if (v == btn4)
            {
                setContentView(R.layout.main);
            }
        }
    } ;    

似乎有什么问题?!:'((

4

0 回答 0