Unable to start an activity on button click - java

I have a button on main activity which onclick is supposed to start a activity
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View v){
if(v.getId()==R.id.lbutton)
{
Intent i = new Intent(MainActivity.this,Display.class);
startActivity(i);
}
}
}
lbutton is the id of button
Display.java
public class Display extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondsc);
}
}
secondsc.xml is a layout file which contains content for the new activity
Manifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Display">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

remove intent-filter to DisplayActivity in your manifest file:
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
And register your clickListener button
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.lbutton).setOnClickLIstener(new OnClickLIstener (){
void onClick (View v) {
onButtonClick(v);
}
});
}

You need to add an event listener to the button. You can either implement the View.OnClickListener interface and implement the onClick method (good if you have multiple buttons that need listeners), or use an anonymous class:
btn.setOnClickListener(new View.OnClickListener() {
onClick (View v) {
}
}

Are you sure that the OnButtonClick is being executed? I recommend doing it this way:
Button lButton = (Button) findViewById(R.id.lButton);
lButton.setOnClickListener(new OnClickListener() {
onClick(View view) {
Intent i = new Intent(MainActivity.this,Display.class);
startActivity(i);
}
}
-Daniel

Related

Open android service or activity on boot api 29

this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.openboot">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".StartAppOnBoot" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
</manifest>
My BroadcastReceiver that "receives" the boot completed intent:
public class StartAppOnBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
My MainActivity that I need to open:
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I need MainActivity to open when the phone turns on, but I don't see the way to do it.
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new
Intent(getApplicationContext,StartAppOnBoot.class);
sendBroadcast(intent);
}
reference
https://developer.android.com/guide/components/broadcasts#java

Unable to open a package(module) inside another package using a button

I have imported a package which was downloaded from the link "https://github.com/moritz-wundke/android-page-curl", then I created a new project and imported the downloaded package by clicking on "import module". I have added a button inside my new package to open the downloaded package. But its not working. Can someone help. Here is the code
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) {
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage
("fi.harism.curl");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
});
}
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:replace="android:icon">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Layout:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:text="press"/>

Class integration in Manifest: application stopped

My manifest.xml raises error for ".RoleActivity". But If I replace my ".roleActivity" with others for checking, they all are okay. Here is my manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zobaed.androidlogin" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".RoleActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".DoctorLoginActivity">
</activity>
<activity android:name=".PatientLoginActivity">
</activity>
</application>
</manifest>
Here is my RoleActivity. Tried to write switch case here.
public class RoleActivity extends AppCompatActivity {
private Button btnPatient;
private Button btnDoctor;
private Button btnGuest;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.log_in_role);
btnPatient = (Button) findViewById(R.id.btpatient);
btnDoctor = (Button) findViewById(R.id.btdoctor);
btnGuest = (Button) findViewById(R.id.btguest);
btnPatient.setOnClickListener((View.OnClickListener) this);
btnDoctor.setOnClickListener((View.OnClickListener) this);
btnGuest.setOnClickListener((View.OnClickListener) this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btdoctor: {
Intent i = new Intent(getApplicationContext(), DoctorLoginActivity.class);
startActivity(i);
break;
}
case R.id.btpatient: {
Intent i = new Intent(getApplicationContext(), PatientLoginActivity.class);
startActivity(i);
break;
}
}
}
}
implement onClickListner in RoleActivity class and change code to
btnPatient.setOnClickListener( this);
btnDoctor.setOnClickListener(this);
btnGuest.setOnClickListener(this);
Your activity is not implementing View.OnClickListener. Unless you implement View.OnClickListener on your activity you cannot cast the activity as an OnClickListener. That is why you are getting error, probably a ClassCastException
btnPatient.setOnClickListener((View.OnClickListener) this);
btnDoctor.setOnClickListener((View.OnClickListener) this);
btnGuest.setOnClickListener((View.OnClickListener) this);
implement View.OnClickListener on your activity. Change
public class RoleActivity extends AppCompatActivity
to
public class RoleActivity extends AppCompatActivity implements View.OnClickListener
and then you can remove that casting
btnPatient.setOnClickListener(this);
btnDoctor.setOnClickListener(this);
btnGuest.setOnClickListener(this);
if you are not implemeting View.OnClickListener on your activity you can add the click listener as an anonymous inner class to handle clicks on views

Changing activity crashes

My app crashes when I try to navigate to another activity. Why does that happen?
I'm able to start the other activity when I launch it at first so there's no problem in the CheckUsernameActivity.
public class CheckNumberActivity extends AppCompatActivity {
EditText phoneNumberEditText;
Button countryCodeButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_number);
Button button = (Button) findViewById(R.id.okButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
countryCodeButton = (Button) findViewById(R.id.countryCodeButton);
phoneNumberEditText = (EditText) findViewById(R.id.phoneNumberEditText);
Log.v("areaCode", countryCodeButton.getText().toString());
Log.v("phoneNumber", phoneNumberEditText.getText().toString());
Intent k = new Intent(CheckNumberActivity.this, CheckUsernameActivity.class);
startActivity(k);
}
});
}
}
Try This way. I think it will help you.
public class CheckNumberActivity extends AppCompatActivity {
EditText phoneNumberEditText;
Button countryCodeButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_number);
Button button = (Button) findViewById(R.id.okButton);
countryCodeButton = (Button) findViewById(R.id.countryCodeButton);
phoneNumberEditText = (EditText) findViewById(R.id.phoneNumberEditText);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v("areaCode", countryCodeButton.getText().toString());
Log.v("phoneNumber", phoneNumberEditText.getText().toString());
Intent k = new Intent(CheckNumberActivity.this, CheckUsernameActivity.class);
startActivity(k);
}
});
}
}
I didn't have my new activity defined in my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dimsumdev.runk" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".activity.CheckNumberActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.CheckUsernameActivity" >
<!--Default Intent Filter-->
<intent-filter>
<action android:name="android.intent.action.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".activity.HomeActivity" >
</activity>
</application>
</manifest>

Android java intents and activities

Bear with me, this is my first Android project.
I've been making a simple Java class, Event, and made the CRUD functions for it. Now I'm doing the layout and can't figure out how I launch these activities after a button is clicked. This is the code I'm working with.
activity_main.xml;
<Button
android:id="#+id/newEventButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="New Event" />
MainActivity.java;
public void addEvent(MenuItem item) {
Intent i = new Intent(this, AddEventActivity.class);
startActivity(i);
}
AndroidManifest.xml;
<activity
android:name="com.example.eventmanager.AddEventActivity"
android:label="#string/title_activity_add_event"
android:parentActivityName="com.example.eventmanager.MainActivity"
android:noHistory="true">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.eventmanager.MainActivity" />
</activity>
What do I need to include into these segments that will link the newEventButton to launch AddEventActivity.class?
In your main activity in onCreate after setContentView add the following code
// get newEventButton
Button addEvent = (Button) findViewById(R.id.newEventButton);
// Listen for button click and start AddEventActivity
addEvent.setOnClickListener(new onClickListener()
{
#Override
public void onClick(View v)
{
startActivity(new Intent(this, AddEventActivity.class);
}
});
You need to add an OnClickListener to the Button in your Activity's Java code. Normally you do this in onCreate().
public class MainActivity extends Activity implements OnClickListener {
public void onCreate(Bundle saved) {
super.onCreate(saved);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.newEventButton);
button.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.newEventButton:
Intent i = new Intent(this, AddEventActivity.class);
startActivity(i);
break;
}
}
}
<activity
android:name="com.example.eventmanager.MainActivity"
android:label="#string/title_activity_add_event" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.eventmanager.AddEventActivity"
android:label="#string/title_activity_add_event"
<intent-filter>
<action android:name="android.intent.action.ADDEVENTACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Here u can work like this also
EDIT
Dont you set your onclick listener if no copy this in oncreate
Button b = (Button) findViewById(R.id.newEventButton);
b.setOnClickListener(new onClickListener()
{
#Override
public void onClick(View v)
{
Intent i = new Intent("android.intent.action.ADDEVENTACTIVITY");
startActivity(i);
}
});
}

Categories