The application now is crashing due to nullpointerexception. I have found many different similar problems on google, however, none of the solutions worked. This app is supposed to forward a call, where the number to receive the forwarded calls are stored within the shared preferences. As for the numbers to forward are stored within a database. If possible, do guide me on how to get data from the database as well. Thanks
These are the logcats:
02-12 15:09:07.745: E/AndroidRuntime(4827): Caused by: java.lang.NullPointerException
02-12 15:09:07.745: E/AndroidRuntime(4827): at com.example.awkwardpanda_redirectcall.MainActivity.onCreate(MainActivity.java:33)
02-12 15:09:07.745: E/AndroidRuntime(4827): at android.app.Activity.performCreate(Activity.java:5158)
02-12 15:09:07.745: E/AndroidRuntime(4827): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-12 15:09:07.745: E/AndroidRuntime(4827): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
02-12 15:09:07.745: E/AndroidRuntime(4827): ... 11 more
This is my mainactivity.java
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Switch switch1 = (Switch) findViewById(R.id.switch1);
EditText editText1 = (EditText) findViewById(R.id.editText1);
SharedPreferences preferences;
preferences = PreferenceManager.getDefaultSharedPreferences(this);
final String streditText1 = preferences.getString("Number", "");
editText1.setText(streditText1);
// set the switch to OFF
switch1.setChecked(false);
// attach a listener to check for changes in state
switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), "Call Forwarding is activated",Toast.LENGTH_SHORT).show();
callforward("*63*" + streditText1 + "#"); // 0123456789 is the number you want to forward the calls.;
}
else{
Toast.makeText(getApplicationContext(), "Call Forwarding is deactivated",Toast.LENGTH_SHORT).show();
callforward("#81#");
}
}
});}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void manage_numbers_onClick(View view) {
Intent myIntent = new Intent(this, Manage_numbers.class);
startActivity(myIntent);
}
private void callforward(String ArrayString)
{
//DBAdapter db = new DBAdapter(this);
//db.getAllContacts().toString();
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager)
this.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Intent intentCallForward = new Intent(Intent.ACTION_CALL);
Uri mmiCode = Uri.fromParts("tel", ArrayString, "#");
intentCallForward.setData(mmiCode);
startActivity(intentCallForward);
}
private class PhoneCallListener extends PhoneStateListener
{
private boolean isPhoneCalling = false;
#Override
public void onCallStateChanged(int state, String incomingNumber)
{
if (TelephonyManager.CALL_STATE_RINGING == state)
{
// phone ringing
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state)
{
// active
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state)
{
// run when class initial and phone call ended, need detect flag
// from CALL_STATE_OFFHOOK
if (isPhoneCalling)
{
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
isPhoneCalling = false;
}
}
}
}
This is my xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="#string/checktoconfirm"/>
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:inputType="phone"
android:text="#string/receivenumbertext" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginTop="10dp"
android:text="#string/savebtn" />
</LinearLayout>
I see no #+id/switch1 in your layout; findViewById() will return null.
UPD: You probably would like to read Re-using Layouts with <include/>
Probably the value of "editText1" is Null which is causing the crash. Check and fix it to incluidng it properly in xml & referncing in the code to remove the crash
Related
I have problems in Android Studio, I don't know how to import the switch ID generically, so I can call this function from all the switches.
I tried to put a description of the switch with the number of it so I can get a number like "1".
public class MainActivity extends AppCompatActivity {
Switch switch1, switch2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switch1 = findViewById(R.id.switch1);
switch2 = findViewById(R.id.switch2);
}
public void OnClick(View view) {
String stateSwitch = "";
if(switch1.isChecked() == false){
stateSwitch = "0";
} else {
stateSwitch = "1";
}
if(switch2.isChecked() == false){
stateSwitch = "0";
} else {
stateSwitch = "1";
}
String idSwitch = switch1.getContentDescription().toString();
// What can I put instead of "switch1" or "switch2" to select the switch generically?
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute("switch", stateSwitch, idSwitch);
}
}
XML of the switches
<Switch
android:id="#+id/switch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="2"
android:onClick="OnClick"
android:text="switch2" />
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="1"
android:onClick="OnClick"
android:text="switch1" />
Sorry for my english.
To get an ID of widget in a code simply get view.getId() it will return a positive integer used to identify the view,
to get id generically you can call
String mId = Integer.toString(view.getId());
when you click on switch1 it will return the id of switch1 similarly when you click on switch2 it will return the id of switch2.
Use checked changed listener and get the id there. For example:
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="152dp"
android:text="Switch1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Switch
android:id="#+id/switch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Switch2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/switch1" />
//////////Java Code///////////
public class MainActivity extends AppCompatActivity {
Switch gSwitch,switch1, switch2;
boolean state;
int id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switch1 = findViewById(R.id.switch1);
switch1 = findViewById(R.id.switch2);
switch1.setOnCheckedChangeListener(checkedChangeListener);
switch2.setOnCheckedChangeListener(checkedChangeListener);
}
CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
id = buttonView.getId();
gSwitch = findViewById(id);// generic switch;
state = isChecked;
// call your method here for example:
// BackgroundWorker backgroundWorker = new BackgroundWorker(this);
// backgroundWorker.execute("switch", state, id);
}
};
}
Use View Binding
For each module that uses view binding, set the viewBinding build option to true in the module-level build.gradle file:
buildFeatures {
dataBinding = true
}
And Remove all imports from kotlinx.android.synthetic.
See more here
I am making simple Android app to load photo from external resources, internal resources or openGL draw.
I tried to load photo with Picasso library for Android, but experiencing:
java.lang.IllegalArgumentException: Target must not be null.
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
at com.gornik.choosephoto.MainActivity$1.onClick(MainActivity.java:62)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10813)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Below is my code for MainActivity.java and activity_photo_from_external_resources.xml:
private RadioGroup radioGroup;
private RadioButton radioButton;
private Button submitBtn;
private ImageView externalPhotoView;
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onClickListener();
}
public void onClickListener(){
radioGroup = (RadioGroup)findViewById(R.id.allOptions);
submitBtn = (Button)findViewById(R.id.button);
submitBtn.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if(radioGroup.getCheckedRadioButtonId() == -1){
Toast.makeText(MainActivity.this, "Select an option, please", Toast.LENGTH_SHORT).show();
}
else {
int selectedOption = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(selectedOption);
Toast.makeText(MainActivity.this,
radioButton.getText().toString(),
Toast.LENGTH_SHORT).show();
String verifyOption = radioButton.getText().toString();
switch (verifyOption) {
case "Photo from external resources":
Intent intent = new Intent(MainActivity.this, PhotoFromExternalResourcesActivity.class);
startActivity(intent);
externalPhotoView = (ImageView)findViewById(R.id.externalImage);
Picasso.with(getApplicationContext()).load(R.drawable.example_image).into(externalPhotoView);
break;
case "Photo from Your resources":
verifyStoragePermissions(MainActivity.this);
Intent intent2 = new Intent(MainActivity.this, PhotoFromYourResourceActivity.class);
startActivity(intent2);
break;
case "OpenGL draw":
Intent intent3 = new Intent(MainActivity.this, OpenGLDrawActivity.class);
startActivity(intent3);
break;
}
}
}
}
);
}
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.gornik.choosephoto.PhotoFromExternalResourcesActivity">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/externalImage"
android:src="#drawable/example_image"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_above="#+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="#+id/button2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
What is the reason for target not null and how can i correct the code?
You can only access Views that are inflated in the current activity.
You are trying to access externalImage inside MainActivity, but externalImage does not exist in activity_main.xml, so the externalPhotoView reference will always be null.
Since the externalImage View is located in activity_photo_from_external_resources.xml, which presumably is inflated inside your PhotoFromExternalResourcesActivity class, you should move the Picasso related code to your PhotoFromExternalResourcesActivity class.
The reference of the ImageView must be null:
externalPhotoView = (ImageView)findViewById(R.id.externalImage);
Thats why you have the exception:
IllegalArgumentException Target must not be null
When try to load the target (.into(externalPhotoView)) :
Picasso.with(getApplicationContext()).load(R.drawable.example_image).into(externalPhotoView);
Ensure that your ImageView with id #+id/externalImage really exists in your activity_main.xml layout.
MainActivity is inflating activity_main.xml and not activity_photo_from_external_resources.xml.
EditText is being a end of year nightmare, and I when finding view by ID, seem to be returned with null when getting text from it.
Settings Activity:
public class Settings extends ActionBarActivity {
String email;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
createPreferences();
}
public void createPreferences(){
SharedPreferences settings = getSharedPreferences("emailAddress", 0);
getData();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
public void getData(){
email = getEmail();
}
public String getEmail(){
setContentView(R.layout.activity_settings);
EditText usernameEditText = (EditText)findViewById(R.id.email_address);
String Username = usernameEditText.getText().toString();
if (Username.matches("")) {
Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
return Username;
}
System.out.println(email + " 8888888888888888888");
if(Username !=null){
email = Username;
}
if (email==null){
SharedPreferences setting = getSharedPreferences("emailAddress", Context.MODE_PRIVATE);
//check preferences.
String emailNew = setting.getString("emailAddress", "n").toString();
if(emailNew != null){
email = emailNew;
}
if(email==null){
//shit
}
}
if(email!= null){
}
return email;
}
}
Settings XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.keepsafe.Settings" >
<EditText
android:id="#+id/email_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/enteremail"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/password_enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/email_address"
android:layout_alignRight="#+id/email_address"
android:layout_below="#+id/email_address"
android:layout_marginTop="33dp"
android:ems="10"
android:hint="Enter password"
android:inputType="textPassword" />
<EditText
android:id="#+id/email_address_toSendto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/enteremail"
android:inputType="textEmailAddress"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/password_enter"
android:layout_below="#+id/password_enter"
android:layout_marginLeft="14dp"
android:layout_marginTop="29dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/SettingsSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/password_enter"
android:layout_alignRight="#+id/password_enter"
android:layout_centerVertical="true"
android:onClick="dataSubmit"
android:text="Submit" />
</RelativeLayout>
I have no idea, and I have been battling this with google for a few hours.
Any help is appreciated, I'm new to this.
Follow the step as mentioned below :
Don't use the setContentView() method inside your getEmail() method.
So the code after the above edits would look as below :
public class Settings extends ActionBarActivity {
String email;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
createPreferences();
}
public void createPreferences(){
SharedPreferences settings = getSharedPreferences("emailAddress", 0);
getData();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
public void getData(){
email = getEmail();
}
public String getEmail(){
//Removed setContentView() from this method
EditText usernameEditText = (EditText)findViewById(R.id.email_address);
String Username = usernameEditText.getText().toString();
if (Username.matches("")) {
Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
return Username;
}
System.out.println(email + " 8888888888888888888");
if(Username !=null){
email = Username;
}
if (email==null){
SharedPreferences setting = getSharedPreferences("emailAddress", Context.MODE_PRIVATE);
//check preferences.
String emailNew = setting.getString("emailAddress", "n").toString();
if(emailNew != null){
email = emailNew;
}
if(email==null){
//shit
}
}
if(email!= null){
}
return email;
}
}
Just a Suggestion : Reference your Views in JAVA after setContentView in your onCreate() method and declare them globally in case you want to access them in many functions.
Hope this Helps!
Its a simple login screen with two edit text one checkbox and a imagebutton ... it was working fine recently i have changed Button to imageButton and now it giving me problem
logcat
04-30 02:11:24.699: E/AndroidRuntime(9571): FATAL EXCEPTION: main
04-30 02:11:24.699: E/AndroidRuntime(9571): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.markitberry/com.example.markitberry.Login}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.EditText
04-30 02:11:24.699: E/AndroidRuntime(9571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
04-30 02:11:24.699: E/AndroidRuntime(9571): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
04-30 02:11:24.699: E/AndroidRuntime(9571): at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-30 02:11:24.699: E/AndroidRuntime(9571): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
here is java
public class Login extends Activity implements AnimationListener {
ImageButton btnLogin;
EditText inputEmail,inputPassword;
CheckBox loginRemember;
// Animation
Animation animBounce1,animBounce2,animBounce3,animBounce4;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// load the animation
animBounce1 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_out);
animBounce2 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_out_right);
animBounce3 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_out);
animBounce4 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_in);
// set animation listener
animBounce1.setAnimationListener(this);
animBounce2.setAnimationListener(this);
animBounce3.setAnimationListener(this);
animBounce4.setAnimationListener(this);
inputEmail=(EditText)findViewById(R.id.etloginEmail);
inputPassword=(EditText)findViewById(R.id.etloginPassword);
loginRemember=(CheckBox)findViewById(R.id.cbRemember);
ImageButton btnLogin =(ImageButton) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
inputEmail.startAnimation(animBounce1);
inputPassword.startAnimation(animBounce2);
loginRemember.startAnimation(animBounce3);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.login_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_call:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:9871952704"));
startActivity(callIntent);
// help action
return true;
case R.id.action_email:
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","markitberry#gmail.com", null));
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "message");
startActivity(Intent.createChooser(intent, "Choose an Email client :"));
case R.id.action_locate:
Intent i = new Intent(Login.this, Locate.class);
startActivity(i);
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
if (animation == animBounce3) {
Intent it=new Intent(Login.this,Home.class);
startActivity(it);
}
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
}
login.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/login_bck"
android:gravity="center">
<include
layout="#layout/login_cover"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
login_cover.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/login_gradient_bck"
android:orientation="vertical" >
<EditText
android:id="#+id/etloginPassword"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_above="#+id/cbRemember"
android:layout_alignLeft="#+id/etloginEmail"
android:layout_alignRight="#+id/etloginEmail"
android:layout_marginBottom="26dp"
android:background="#BFFFFFFF"
android:ems="10"
android:gravity="center"
android:hint="Enter Password"
android:inputType="textPassword"
android:textColor="#FFFFFF" />
<EditText
android:id="#+id/etloginEmail"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_above="#+id/etloginPassword"
android:layout_centerHorizontal="true"
android:layout_margin="30dp"
android:layout_marginBottom="36dp"
android:background="#BFFFFFFF"
android:ems="10"
android:gravity="center"
android:hint="Enter Email"
android:inputType="textEmailAddress"
android:textColor="#FFFFFF" />
<CheckBox
android:id="#+id/cbRemember"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_above="#+id/btnLogin"
android:layout_alignLeft="#+id/etloginPassword"
android:layout_alignRight="#+id/etloginPassword"
android:layout_marginBottom="62dp"
android:text="Remember Me"
android:textColor="#E6E6E6" />
<ImageButton
android:id="#+id/btnLogin"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:background="#drawable/button"
android:src="#drawable/login_icon_2" />
</RelativeLayout>
Maybe you're having this problem because you didn't change your Button control to ImageButton in your view (login.xml). If that's not the case, you should clean your project and build it again, the R.java file sometimes does not refresh it self with the new changes (this issue is kind of weird, but sometimes it happens).
Just for the record... when this kind of problem happens and you have your code with no errors, it's because you changed the position of your control (For example... your Button was at the beginning but after a while you moved it to the end of the layout) for some reason the project doesn't realize this change and still thinks your Button is the first control in the layout.
Sorry to write this suggestion in an answer... I don't have enough points to comment in your question.
Button extends TextView while ImageButton extends ImageView. Kind of wierd, but that's Android for you.
I would suggest possibly using Button then using android:background="#drawable/MyDrawable" and see if that will achieve what you are trying to do.
You are using the ImageButton two times in you class:
ImageButton btnLogin;
And;
ImageButton btnLogin =(ImageButton) findViewById(R.id.btnLogin);
Please remove the ImageButton btnLogin; if you are using the ImageButton somewhere else..
Secondly, note that you are not registering the ImageButton anywhere in the onCreate()..
My suggestion, just remove the ImageButton btnLogin; before the onCreate() and try it.
Or change this :
ImageButton btnLogin =(ImageButton) findViewById(R.id.btnLogin);
to:
btnLogin =(ImageButton) findViewById(R.id.btnLogin);
Should be working now.
I'm trying to send a message from my app using as SMS content the content of a editText. I tryed in this way so far:
Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
String content = edit.getText().toString();
it.putExtra("sms_body", content);
startActivity(it);
but when the activity starts there is no content in the message.. isn't possible do something like that?
Try to send message like this
String phoneNo = "080000123";
String sms = textsms.getText().toString();
try
{
android.telephony.SmsManager smsmanager = android.telephony.SmsManager.getDefault();
smsmanager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(),"SMS faild, please try again later!",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
I tried using the same code of yours, but within a method call. This is because if you have more than 1 messaging app installed on your device you will have to manually select the app that you are going to send your message with.
In my case I have whatsapp, messaging, etc. So having your code in the onCreate(), it will only fail as there is no option selected by the user...But when I have this code within a method body and I trigger with a button or any other even similarly, it works fine(only thing is you will have to select the app-you can make any of the app as default so that the next time you don't have to do this manually).
My MainActivity.java
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View v)
{
Uri uri = Uri.parse("smsto:9999999999");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
EditText edit = (EditText) findViewById(R.id.editText1);
String content = edit.getText().toString();
it.putExtra("sms_body", content);
startActivity(it);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
My activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="send" />
</LinearLayout>