打开新活动时出现问题 [英] Problem in opening a new activity

查看:169
本文介绍了打开新活动时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,我在点击按钮时打开新活动时遇到问题。按下按钮时,我需要我的程序打开一个新活动。这就是我所做的:



在xml中我创建了一个按钮



Sir, I have a problem in opening a new activity on clicking the button. I need my program to open a new activity when my button is pressed.Here is what I have done:

In xml I have created a button

<Button

        android:layout_width="500dp"

        android:layout_height="50dp"

        android:text="testButton"

        android:id="@+id/button"

        android:layout_below="@+id/imageView"

        android:layout_centerHorizontal="true"

        android:background="@android:color/holo_red_light" />



我创建了一个名为ship的新空活动。



在我的主要.java中




I have created a new empty activity named "ship".

In my main .java

final Button btn = (Button) findViewById(R.id.button);
      btn.setOnClickListener(new View.OnClickListener() {

                                 @Override
                                 public void onClick(View v) {
                                     Intent intent = new Intent(this, ship.class);
                                     startActivity(intent);

                                 }
                             }
      );





我已经添加了清单文件



And I have added the manifest file

</activity>
       <activity android:name=".ship" >
       </activity>





我的程序显示错误。

[更新]

这是我的错误消息



And my program shows error.
[update]
This is my error message

Quote:

错误:(20,56)错误:没有为Intent找到合适的构造函数(< anonymous onclicklistener =>,Class< s臀部>)

构造函数Intent.Intent(String,Uri)不适用

(参数不匹配; < anonymous onclicklistener =>无法转换为String)

构造函数Intent.Intent(Context,Class(参数不匹配;< anonymous onclicklistener =>无法转换为Context)

Error:(20, 56) error: no suitable constructor found for Intent(<anonymous onclicklistener="">,Class<ships>)
constructor Intent.Intent(String,Uri) is not applicable
(argument mismatch; <anonymous onclicklistener=""> cannot be converted to String)
constructor Intent.Intent(Context,Class(argument mismatch; <anonymous onclicklistener=""> cannot be converted to Context)



我错过了什么吗?







请帮助我。


Have I missed anything?



Kindly help me.

推荐答案

这项工作对我来说



Main.java



This work for me

Main.java

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


    final Button btn = (Button) findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Main.this, ship.class); // change this line and you will good to go
            startActivity(intent);

        }
    });
}





Ship.java







Ship.java


public class ship extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ship);

    }
}


这篇关于打开新活动时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆