Click here to Skip to main content
15,910,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

XML
<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" />

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

In my main .java

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
XML
</activity>
       <activity android:name=".ship" >
       </activity>


And my program shows error.
[update]
This is my error message
Quote:
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.
Posted
Updated 18-Dec-15 5:32am
v2
Comments
phil.o 18-Dec-15 11:28am    
"Have I missed anything?"
Yes, you did not describe the error itself :)
This is not your first question here, and you should have learnt how to ask a good question for now. Please have a look at the FAQ.
[no name] 20-Dec-15 1:50am    
I am really sorry Sir. I have updated my question.Thank you.
[no name] 23-Dec-15 12:55pm    
Sir, I have tried to solve my question many times but I cannot spot out my mistakes. Can you please help me with this?

I have encountered the same error for all my try.
phil.o 24-Dec-15 4:10am    
I don't do Android development, so I am not going to answer on something which is obscure to me.
Error messages seem to point to a constructor used with a parameter that cannot be converted to string. You have to debug your application and investigate why you get this exception. I cannot do that for you.
You should google for "android debugging" and start from there. Good work :)
[no name] 25-Dec-15 22:13pm    
Thank you for your kind suggestion Sir. :)

1 solution

This work for me

Main.java

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


Java
public class ship extends AppCompatActivity{

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

    }
}
 
Share this answer
 
v4
Comments
[no name] 25-Dec-15 22:11pm    
Sir, this was the same solution suggested by my debugger.But it is not working.I have created the "ship" class and I did not modify anything.Can you please explain me that with what code should I update my question.Thank you.
wseng 26-Dec-15 0:08am    
post full code here and remove the final. After used the code suggested by debugger, what error you get?
[no name] 26-Dec-15 4:00am    
This is my "ship" class

public class ship extends AppCompatActivity {

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

}
}

and I get the same error on using the code suggested by the debugger.

My error message occurs in this line

Intent intent = new Intent(this, ships.class);

Thank you.
wseng 26-Dec-15 4:22am    
My error message occurs in this line
Intent intent = new Intent(this, ships.class);


Why you still using Intent intent = new Intent(this, ships.class); ??? Check my answer now
[no name] 26-Dec-15 5:56am    
Thank you for your kind help sir but now my generated ".apk" fails to open the new window by popping up "Unfortunately pictures has stopped"
pictures = my application name

Contents in the main activity opens successfully and the build is successful.What should I do now?

Thank you once again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900