如何在方法内编写switch语句

时间:2014-04-07 04:23:48

标签: android

我有7项活动A.B,C,D,E,F,G。当我点击任何这些按钮时,例如:如果我点击A,我想去那个活动。我想在Switch case中使用方法来做。我尝试过这样做,但我的应用程序崩溃了:

这段代码出了什么问题。它崩溃了吗?

package com.example.assignment2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Act_A extends ActionBarActivity {
    TextView text;
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_);
        text = (TextView) findViewById(R.id.display);
        text.setText("This is Activity A");
        text.setTextSize(20);
    }
    public void Display(View v) {
        switch (btn.getId()) {
        case R.id.butA:
            Intent intent = new Intent(this, Act_A.class);
            startActivity(intent);
            break;
        case R.id.butB:
            Intent intent1 = new Intent(this, Act_B.class);
            startActivity(intent1);
            break;
        case R.id.butC:
            Intent intent2 = new Intent(this, Act_C.class);
            startActivity(intent2);
            break;
        case R.id.butD:
            Intent intent3 = new Intent(this, Act_D.class);
            startActivity(intent3);
            break;
        case R.id.butE:
            Intent intent4 = new Intent(this, Act_E.class);
            startActivity(intent4);
            break;
        case R.id.butF:
            Intent intent5 = new Intent(this, Act_F.class);
            startActivity(intent5);
            break;
        case R.id.butG:
            Intent intent6 = new Intent(this, Act_G.class);
            startActivity(intent6);
            break;
        }
    }
    public void NextActivity(View v) {
        Intent intent = new Intent(this, Act_B.class);
        startActivity(intent);
    }
}

5 个答案:

答案 0 :(得分:3)

您未在Button btn;中初始化onCreate,因此您获得NullPointerException

如果您已在xml中声明了单击侦听器,那么您应该:

switch (v.getId()) {
// cases
}

答案 1 :(得分:0)

试试这个..

btn.getId()

中的v.getId()更改为switch
public void Display(View v) {
    switch (v.getId()) {

答案 2 :(得分:0)

public void Display(View v) {
    switch (btn.getId()) {

--------------

}

}

btn.getId()更改为 v.getId()

您正在尝试访问刚刚声明按钮btn 的未初始化对象 btn 的ID。

做这样的事情

public class HomeActivity extends ActionBarActivity {

private TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    text = (TextView) findViewById(R.id.display);
    text.setText("This is home activity");
    text.setTextSize(20);
}

public void Display(View v) {

    Intent mIntent = null;
    switch (v.getId()) {

    case R.id.butA:
        mIntent = new Intent(this, AboutActivity.class);
        break;

    case R.id.butB:
        mIntent = new Intent(this, InformationActivity.class);
        break;

    case R.id.butC:
        mIntent = new Intent(this, ContactActivity.class);
        break;

    case R.id.butD:
        mIntent = new Intent(this, PostsActivity.class);      
        break;
    }

    if(mIntent != null){
        startActivity(mIntent);
    //if you wanted to finish this activity uncomment following line
    //finish();
    }

}


@Override
public void onBackPressed() {    
    super.onBackPressed();
}

}

如果您想调用活动并完成当前活动,则应调用完成方法

Intent mIntent = new Intent(HomeActivity.this, AboutActivity.class);
startActivity(mIntent);
finish();

如果您想在后退按钮上完成当前活动,

@Override
public void onBackPressed() {  

//write the functions you need to do before finishing the activity here
// if you written this line **super.onBackPressed();** which finish the activity on back press
   super.onBackPressed();
}

答案 3 :(得分:0)

您尚未初始化按钮。

Button btnA;

//

btnA= (Button) findViewById(R.id.btnA);

还导入onclicklistner

btnA..setOnClickListener(this);

并使用像这样的开关语句

  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btnA:

答案 4 :(得分:0)

试试这个

             public void Display(View v) {
                switch (v.getId()) {
                case R.id.btn_A :
                        Intent intent1 = new Intent(this, Act_A.class);
                        startActivity(intent1);
                        break;

               case R.id.btn_B :
                        Intent intent2 = new Intent(this, Act_B.class);
                        startActivity(intent2);
                        break;
                     case R.id.btn_C :
                        Intent intent3 = new Intent(this, Act_C.class);
                        startActivity(intent3);
                        break;
                     case R.id.btn_D :
                        Intent intent4 = new Intent(this, Act_D.class);
                        startActivity(intent4);
                        break;
                     case R.id.btn_E :
                        Intent intent5 = new Intent(this, Act_E.class);
                        startActivity(intent5);
                        break;
                     case R.id.btn_F :
                        Intent intent6 = new Intent(this, Act_F.class);
                        startActivity(intent6);
                        break;
                      case R.id.btn_G :
                        Intent intent7 = new Intent(this, Act_G.class);
                        startActivity(intent7);
                        break;


        }
        }
试试这个,我还没试过,但我希望它会起作用...... 在Display Method中,而不是传递View参数传递int参数。

public void Display(int btn_id)
{
switch (btn_id) {
//same as above
}
}

我猜你会在A活动中编写这个方法 从点击按钮B的不同活动,调用此方法并发送按钮B的ID 作为参数,看它是否有效。