Im trying to make an app with buttons to call different phone numbers. I have this:
package com.BigTooth.Apps.Recromax;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.Intent;
import android.content.*;
import android.net.Uri;
import android.view.View.OnClickListener;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
private void phoneCall1()
{
String phoneCallUri = "tel:4078421430";
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
startActivity(phoneCallIntent);
}
// add button listener
button.setOnClickListener(new OnClickListener() {
private void phoneCall()
{
String phoneCallUri = "tel:8889807091";
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
startActivity(phoneCallIntent);}
}
}
In my MainActivity.java file. Its telling me that isn't correct. Please help!!!
button.setOnClickListener is in the wrong place. Also, the onClickListener is a bit wring. Should be:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.Intent;
import android.content.*;
import android.net.Uri;
import android.view.View.OnClickListener;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.youButtonId);
Button button1 = (Button) findViewById(R.id.youButtonId1);
// add button listener
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
phoneCall();
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
phoneCall1();
}
});
}
private void phoneCall()
{
String phoneCallUri = "tel:8889807091";
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
startActivity(phoneCallIntent);
}
private void phoneCall1()
{
String phoneCallUri = "tel:4078421430";
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
startActivity(phoneCallIntent);
}
}
Related
I got an while i am doing my code
My Error is :
Unable to instantiate activity ComponentInfo{com.example.example/com.example.sample.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
import androidx.appcompat.app.AppCompatActivity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.jaredrummler.android.processes.AndroidProcesses;
import com.jaredrummler.android.processes.models.AndroidAppProcess;
import com.jaredrummler.android.processes.models.Stat;
import com.jaredrummler.android.processes.models.Statm;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<RunningApplication> runningApplicationList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
Button send = (Button) findViewById(R.id.button);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getRunningApps();
}
});
}
public void getRunningApps() {
List<AndroidAppProcess> processes = AndroidProcesses.getRunningAppProcesses();
PackageManager pm = getPackageManager();
for (AndroidAppProcess pro : processes) {
try {
String proccessname = pro.name;
Stat stat = pro.stat();
int pid = stat.getPid();
int parentProccessId = stat.ppid();
long startTime = stat.stime();
int policy = stat.policy();
char state = stat.state();
Statm statm = pro.statm();
long totalSizeofProccess = statm.getSize();
long residentSetSize = statm.getResidentSetSize();
PackageInfo packageInfo = pro.getPackageInfo(MainActivity.this,0);
//get the app name
String appName = packageInfo.applicationInfo.loadLabel(pm).toString();
//Get the app icon
Drawable appIcon = packageInfo.applicationInfo.loadIcon(pm);
//Add it to your list of running app
RunningApplication ra = new RunningApplication(appName,startTime,String.valueOf(pid),appIcon);
runningApplicationList.add(ra);
Log.e("APPNAME : ", appName);
} catch (Exception ex) {
Log.e("APPNAME.CONTEXT",ex.getMessage());
}
}
}
}```
You're getting NPE because you are trying to create a new instance of your button before calling setContentView(R.layout.activity_main);
This is what is causing your app to crash.
Consider changing your onCreate method to this snippet below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = (Button) findViewById(R.id.button);
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getRunningApps();
}
});
}
I have the code for the sound and the next activity. I do not know how to merge the two codes in order to execute them at the same time or in sequence.
package com.example.testmessages;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Add sound
final MediaPlayer gunSoundMP = MediaPlayer.create(this, R.raw.gunsound);
ImageButton GunSound=(ImageButton)this.findViewById(R.id.imageButton3);
GunSound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
gunSoundMP.start();
}
});
//Image button to execute new Activity
imageButton=(ImageButton) findViewById(R.id.imageButton3);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentLoadMain2Activity = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intentLoadMain2Activity);
gunSoundMP.start();
}
});
}
}
I have 2 screen on first i have Text field and when i pressed the button it takes me to second screen with data from the Text Field but while pressing the back button from screen 2 to screen 1.
I want that the text field(screen 1) will show the previously added data through variables.
////////////////////////////////////Screen 1 code://///////////////////////////
package com.example.abids.savingdataonbackbutton;
import android.content.Intent;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button button;
EditText name;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.buttonNext1);
name=(EditText)findViewById(R.id.editTextName);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String namevalue= name.getText().toString();
savedInstanceState.putString("MyString", "Welcome back to Android");
Intent intent=new Intent(MainActivity.this,Main3Activity.class);
intent.putExtra("Name",namevalue);
startActivity(intent);
}
});
}
}
////////////////**Screen 2 code:**////////////////////////////////////////////
package com.example.abids.savingdataonbackbutton;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
public class Main3Activity extends AppCompatActivity {
TextView t1;
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
b1=(Button) findViewById(R.id.button);
t1=(TextView)findViewById(R.id.textView2);
t1=(TextView)findViewById(R.id.textView2);
getIntent().getStringExtra("Name");
t1.setText("Name :" +getIntent().getStringExtra("Name"));
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent= new Intent(Main3Activity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
Screen 2
package com.example.abids.savingdataonbackbutton;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
public class Main3Activity extends AppCompatActivity {
TextView t1;
Button b1;
String valueOfName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
b1=(Button) findViewById(R.id.button);
t1=(TextView)findViewById(R.id.textView2);
t1=(TextView)findViewById(R.id.textView2);
getIntent().getStringExtra("Name");
valueOfName = getIntent().getStringExtra("Name");
t1.setText("Name :" +getIntent().getStringExtra("Name"));
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
screen2Done();
}
});
}
public void screen2Done() {
Intent intent=new Intent();
intent.putExtra("RESULT_STRING", valueOfName);
setResult(RESULT_OK, intent);
finish();
}
#Override
public void onBackPressed() {
screen2Done();
}
On screen 1, capture the value in onActivityResult() method, similarly as in screen 2.
I am fairly new to Android/Java developing and I have run into this error. I cannot figure out what is wrong after doing extensive research and playing with my methods. Here is my code:
package org.flinthill.finalprojectv2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
public class mainactivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
final Button SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener()
{
new View.OnClickListener() {
#Override
public void onClick(View view){
}
};
}
}
If anyone knows what I could've done wrong, please let me know! Thanks!
Put the code inSide onCreate()
public class mainactivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Button Clicked
}
});
}
}
put your code inside onCreate() method
see the android lifeCycle to understand why :https://developer.android.com/guide/components/activities/activity-lifecycle.html
Your code is out of your method, Change it to this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener()
{
new View.OnClickListener() {
#Override
public void onClick(View view){
}
};
}
}
Do Button initialization and adding listener to Button inside onCreate() method.
Try this:
package org.flinthill.finalprojectv2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
public class mainactivity extends AppCompatActivity {
Button SuSe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something
}
});
}
}
I finally learned about adding a button to a page and actually making it navigate to another activity "XML Page". Anyway, I have been trying to add 2 buttons in the same page which navigate each to a different XML's Pages. All I did was copy the first button which worked and then change the button name and all other things the first button works but the second isn't. It shows a click but nothing happens after.
Back1 Button works. TMode Button does the trouble.
Eclipse is not showing errors.
Here is my code -
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 GameMode extends Activity {
/** Called when the activity is first created.*/
Button btn;
Button btn1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn=(Button)findViewById(R.id.Back1);
btn.setOnClickListener(btn2Listener);
}
private OnClickListener btn2Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent2=new Intent(GameMode.this,MainActivity.class);
startActivity(intent2);
}
};
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn=(Button)findViewById(R.id.TMode);
btn.setOnClickListener(btn3Listener);
}
private OnClickListener btn3Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent3=new Intent(GameMode.this,CharacterSelect.class);
startActivity(intent3);
}
};
}
Try something like this:
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 GameMode extends Activity {
Button btn1;
Button btn2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn1=(Button)findViewById(R.id.Back1);
btn1.setOnClickListener(btn1Listener);
btn2=(Button)findViewById(R.id.TMode);
btn2.setOnClickListener(btn2Listener);
}
private OnClickListener btn1Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent(GameMode.this,MainActivity.class);
startActivity(intent2);
}
};
private OnClickListener btn2Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent(GameMode.this,CharacterSelect.class);
startActivity(intent2);
}
};
}
You should in your XML file define two buttons
<Button
android:id="#+id/button1"
... />
<Button
android:id="#+id/button2"
... />
And then in your Activity in onCreate() method you do
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
...
})
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
...
});
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 GameMode extends Activity {
Button btn1;
Button btn2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn1=(Button)findViewById(R.id.Back1);
btn1.setOnClickListener(btn1Listener);
btn2=(Button)findViewById(R.id.TMode);
btn2.setOnClickListener(btn2Listener);
}
private OnClickListener btn1Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent(GameMode.this,MainActivity.class);
startActivity(intent1);
}
};
private OnClickListener btn2Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent2=new Intent(GameMode.this,CharacterSelect.class);
startActivity(intent2);
}
};
}