0

我想,当你点击打开一个新屏幕(Activity),但有问题时,新屏幕打不开。对代码做了哪些修改,让新窗口打开了?

package com.android.yalt;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

  Button btnny;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnny = (Button) findViewById(R.id.ny);
    btnny.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.ny:
        startActivity(NY.class);
      break;
    default:
      break;
    }
  }

private void startActivity(Class<NY> class2) {
    // TODO Auto-generated method stub

}
}
4

1 回答 1

0

你必须使用

startActivity(new Intent(getBaseContext(), NY.class));

或者

Intent i = new Intent(getBaseContext(), NY.class);
startActivity(i);

或以方法格式:

 public void startMyActivity(Class activityToStart){
startActivity(new Intent(getBaseContext(), activityToStart.class));
    }

就像旁注一样,如果想要例如,可以将 getBaseContext() 替换为您自己的上下文startActivity(new Intent(yourOnGoingActivityGoesHere.this, yourClassGoesHere.class));

也作为一个实用程序:

/*This is not tested but should work */
public class AndroidUtility(){
/**
 *Usage: AndroidUtility.startMyActivity(MainActivity.this, NY.class);
 */
public static void startMyActivity(Activity onGoingActivity, Class activityToStart){
onGoingActivity.startActivity(new Intent(onGoingActivity.getBaseContext(), activityToStart.class));


   }
}

在您的情况下,最简单的是:

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.ny:
          startActivity(new Intent(getBaseContext(), NY.class));
      break;
    default:
      break;
    }
  }
于 2013-10-16T16:58:44.363 回答