繁体   English   中英

需要帮助完成这两个活动的代码吗?

[英]Need help finishing the code for these two activities?

我正在开发一个使用on click监听器启动两个活动的android应用,我的代码中的所有内容都可以正常检查,除非在public void onClick(View v)开始的地方,我从该行开始有多个错误,并且无法运行代码? 我将不胜感激,对此我将不胜感激。 我的代码如下

public class Safaricom extends Activity {

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

          Button button1 = (Button)findViewById(R.id.button1);
          Button button2 = (Button)findViewById(R.id.button2);

          button1.setOnClickListener(buttonClickListener);
          button2.setOnClickListener(buttonClickListener);
      }

      private OnClickListener buttonClickListener = new View.OnClickListener() {

           @Override
             public void onClick(View v) {
                  Intent intent = null;
                  switch(v.getId()){
                  case R.id.button1: 
                       intent = new Intent(this, Second.class);
                       break;
                  case R.id.button2:
                       intent = new Intent(this, SignUp.class);
                       break;
                  }
                  if (intent != null)
                      this.startActivity(intent);
             }     
      }; 
}

错误有两点。 首先,它public void on click viewpublic void on click view (错误是- Multiple Markers at this line - implements android.view.View.OnClickListener.onClick- The method onClick(View) of type new View.OnClickListener(){} must override a superclass

第二个地方说this.startActivity(intent); (错误是- he method startActivity(Intent) is undefined for the type new View.OnClickListener(){}

取而代之的this使用v.getContext()YOUR_ACTIVITY.this

其实如果你仔细阅读文档,你就会知道, Intent参数包含Activity ,所以当你使用this就意味着你是给类型的参数, new View.OnClickListener

好吧,我马上就能看到几个错误。 使其变得更加清晰,因为仅通过查看代码和学习显然并不清楚。 我在每个新的Intent语句中添加了Safaricom.this。 这是因为Intent构造函数需要将Context作为第一个参数,而OnClickListener不是Context,因此您需要对作为上下文的活动进行ge封装。 我错过了另一项编辑,startActivity也需要预先添加Safaricom。

public class Safaricom extends Activity {

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

      Button button1 = (Button)findViewById(R.id.button1);
      Button button2 = (Button)findViewById(R.id.button2);

      button1.setOnClickListener(buttonClickListener);
      button2.setOnClickListener(buttonClickListener);
  }

  private OnClickListener buttonClickListener = new View.OnClickListener() {

       @Override
         public void onClick(View v) {
              Intent intent = null;
              switch(v.getId()){
              case R.id.button1: 
                   intent = new Intent(Safaricom.this, Second.class);
                   break;
              case R.id.button2:
                   intent = new Intent(Safaricom.this, SignUp.class);
                   break;
              }
              if (intent != null)
                  Safaricom.this.startActivity(intent);
         }     
     };
 }

对于第一个错误

The Error is - Multiple Markers at this line - implements android.view.View.OnClickListener.onClick- The method onClick(View) of type new View.OnClickListener(){} must override a superclass )

尝试删除@override

如果这样不能消除第二个错误,请告诉我们是否出现其他问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM