I was working in app with 2 interface.
every thing was good until i tried to put a button that take you
to the #2 interface the app crash when i press the button in the emulator
Can someone help explain how to do that ?
Button mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(mIntent);
}
});
so what is wrong?
Related
for example i have made a Nodemcu server and there are 3 links on the main webpage(192.168.18.100).
the thing i want to try is that when i click an android button this link get accessed without opening any new activity i.e i want to stay on the main activity.
this is my code so far that works but not according to what i want it.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button on = (Button) findViewById(R.id.button);
Button off =(Button) findViewById(R.id.button2);
Button pink = (Button) findViewById(R.id.button3);
on.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://192.168.18.100/ir?code=16236607"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_SEND, uri);
startActivity(intent);
}
});
off.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://192.168.18.100/ir?code=16203967"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
pink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("http://192.168.18.100/ir?code=16214167"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}
You can try to implement a WebView on your MainActivity. When you click on any of your button, load the WebView with your desired url.
You can find the way to implement WebView in this link
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSighin = (Button) findViewById(R.id.btnSignIn);
btnSignUp = (Button) findViewById(R.id.btnSignUp);
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent j = new Intent(MainActivity.this, SignUp.class);
startActivity(SignUp);
}
});
btnSighin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View view) {
Intent k = new Intent(MainActivity.this, SignIn.class);
startActivity(SignIn);
}
});
}
I am working on an app that allows customers to book appointments and in order to do so, they need to sign up or sign in. I have activities made for both of them and I can't run it without getting the "Expression Expected" and I'm new to Android Studio and have no idea what to do. Any help?
You are missing some information. Anyway, i see that you have mistakes in lines
startActivity(SignUp);
and
startActivity(SignIn);
The correct way should be startActivity(j) and startActivity(k) respectively
You have to pass the intent object to the start activity method not the signup or signin class
So it should be startActivity(i) and startActivity(j)
How to solve this problem? When I restart activity the array works anew and picture doesnt changes.I want to change the picture to follow from array when I restart activity. This button restarts activity
Button btnNext = (Button) dialog.findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
dialog.dismiss();
finish();
startActivity(getIntent());
}
});
I've a simple problem.I'm trying to switch layout between Main Menu and About pages.In Main Menu, there is no problem when i click the "about" button.But in "about" layout, when i click "return to menu" button it just doesn't work.and the code of that layout is in about.java, which also extends Activity.Please have a look.
in MainActivity.java:
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), About.class);
setContentView(R.layout.about);
}
});
works just fine.But in About.java:
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
});
nothing happens.I tried every combination of inside onClick() but just doesn't work.What are your ideas?Thanks and have a nice day.
In Main Activity,java, it's not starting any activity, it's basically just changing the view. It seems to be working but actually it's not.
You should declare the intent and then call the start activity method. The other activity should have a method onCreate where you can set the content view (using the method setContentView).
It should be something like this:
MainActivity.java
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), About.class);
startActivity(intent);
}
});
About.java
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
});
For more information, check this http://developer.android.com/training/basics/firstapp/starting-activity.html
Try to do the same like in your MainActivity in your AboutActivity:
button1_.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), MainActivity.class);
setContentView(R.layout.activity_main); //should be without this line if you set the layout in your onCreate method in the MainActivity (respectively AboutActivity)
startActivity(intent);
}
});
If it works once, should work twice as well
I am developing an android app and in that i want to hyperlink the youtube video by button so that when i click that button a particular url will open which i provide to it. I want to build many buttons so also tell how to hyperlink a button with another button.
Help me out.
You can set an onClickListener on your button which will start the Intent:
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com/"));
startActivity(i);
}
});