Open Another Activity in Android - java

I am new to Android Development and I have this problem of opening another activity from the main activity.
The problem is that whenever I click on the button, the app closes.
Below is my Java code.
package com.Notely.SplashScreenandAccounts;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ActivityWelcome extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Button signup_btn = (Button) findViewById(R.id.signup_btn);
signup_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, ActivitySignup.class);
startActivity(intent);
}
}
I need suggestions on what is the problem and on how to rectify that.

You should add your Activity(ActivitySignup.class) in your
AndroidManifest file.
Like this..
<Activity android:name = .ActivituSignup android:theme="AppTheme"/>
i hope this will help you .
If it not works you should add this line in your ActivitySignup.java at on create before super.onCreate()
setTheme(R.style.AppCompat);
send your logcat error to understanding best.
You should take a tour of Activity in
android.developers.com web

Related

event can't run in android ( get errors)

I recently use android studio instead of eclipse.
but somthing is wrong!
these are main java code that run currectly in eclipse but now get errors!
package com.example.amir.myapplicationeeee;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new Intent(getApplicationContext(),Main2Activity.class));
finish();
}
});
setOnClickListener is hilighed red and error ";" excepted showed when want to runnin app in virtual device
If the code you provide here is exactly the same as you have in your project, then you should just add '}' on the end of your code - it closes the MainActivity class.
You should also have evertyhing implemented inside onCreate() method.
So it should look like:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new
Intent(this,Main2Activity.class));
finish();
}
});
}
}

Application crashes on clicking a link in TextView

I am working on an application called Android Glossary. In my second activity I have created a TextView which has a link embedded in it. Everything seems fine; the link is highlighted in blue. The problem is that when I click on the link, my application crashes down. I don't know what is wrong. Please help.
Code in my second activity :
package com.mavenmaverick.androidglossary;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class CActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
TextView link = (TextView) findViewById(R.id.link);
link.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Uri adress= Uri.parse("http;//www.cyanogenmod.com");
Intent browser= new Intent(Intent.ACTION_VIEW, adress);
startActivity(browser);
}
});
}
see your code
you have enter url like
Uri adress= Uri.parse("http;//www.cyanogenmod.com");
replace with this
Uri adress= Uri.parse("http://www.cyanogenmod.com");
problm is ; in Http; replace with http:
you should use setData()
String url = "http://www.cynogenmod.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
TextView link = (TextView) findViewById(R.id.link);
link.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browser = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.cyanogenmod.com"));
startActivity(browser);
}
});

android devolpment linking buttons to other xml pages

I'm developing an app for android. The main screen has 6 buttons. Each button leads to another screen. I'm having trouble with the code to make the buttons do anything when clicked. this is what I have:
on the main page my button id is glass the page opened when clicked is glass.xml
android:onClick="Intent i = new Intent(FirstActivity.this, SecondActivity.class);"
and my scr folder I have the java activities FirstActivity.java
package install.fineline;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fine_line);
Button btnload = (Button) findViewById(R.id.glass);
btnload.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
}
and SecondActivity.java
package install.fineline;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.glass);
}
}
what am i doing wrong?
On the button definition in the xml remove the onClick attribute.
The button click action is set in code so it doesn't need to be in the xml file.
There is sample code here showing how to do what you want.

Change between activities does not work

I'm trying to change between activities in my Android app (2.1-update1), but it doesn't work.
Any suggestions?
The only thing that happens when I debug the app is that it stops on this part of the code in Instrumentation.java:
public void waitForIdle() {
synchronized (this) {
while (!mIdle) {
try {
wait();
} catch (InterruptedException e) {
}
}
}
}
Eclipse says that it is in Thread 1 on
Instrumentation.checkStartActivityResult(int, Object) line: 1537. If I
resume the app, the next stop is in ZygoteInit.java trying to run
Throwable cause = ex.getCause(); ... Eclipse says
ZygoteInit$MethodAndArgsCaller.run() line: 864.
Here is the source code:
HappyHomes.java
package com.example.app;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HappyHomes extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button login = (Button) findViewById(R.id.btnLogin);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ProgressDialog laddRuta = ProgressDialog.show(HappyHomes.this, "",
"Loggar in, vänligen vänta...", true);
Intent myIntent = new Intent(view.getContext(), Kategorier.class);
myIntent.
startActivity(myIntent);
}
});
}
}
Kategorier.java
package com.example.app;
import android.app.Activity;
import android.os.Bundle;
public class Kategorier extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kategorier);
}
}
Thanks for helping!
Make sure that Kategorier is registered in your AndroidManifest.xml file.
Change myIntent.startActivity(myIntent); to HappyHomes.this.startActivity(myIntent);
There is no any startActivity() method in Intent class . you must be doing wrong.
just write startActivity(myIntent)
All the Services, Broadcast Receivers and Activites must be declared in manifest file.

Runtime error while switching between activities in Android

I got a runtime error when I press the button that should change the activity:
package com.example.LocationTracker;
import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button;
public class LocationTracker extends Activity{ /** Called when the activity is first created. */
Button btn_Tracker;
Button btn_Display_Map;
Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext();
btn_Tracker = (Button)findViewById(R.id.btn_Tracker);
btn_Tracker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//setContentView(R.layout.trackeractivity);
Intent myIntent1 = new Intent(view.getContext(), TrackerActivity.class);
context.startActivity(myIntent1);
}});
}
class TrackerActivity extends Activity {
//Your member variable declaration here
// Called when the activity is first created.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.trackeractivity);
}
}
I added everything right in the maniefest file
<activity android:name=".TrackerActivity" android:label="#string/app_name"/>
<activity android:name=".DisplayMapActivity" android:label="#string/app_name"/>
</application>
Any idea?
I think TrackerActivity needs to be public, which means it will need to be in its own file as well.
You shouldn't be using getApplicationContext() to start activities. Every activity is a context, so having a member instance of Context should not be necessary. Try re-writing the onClick method of your OnClickListener like this
public void onClick(View view) {
Intent myIntent1 = new Intent(LocationTracker.this, TrackerActivity.class);
LocationTracker.this.startActivity(myIntent1);
}});
Also, refer to this documentation for when to use the application context.

Categories